use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class Question method main.
public static void main(String[] args) {
LinkedListNode head = AssortedMethods.randomLinkedList(10, 0, 10);
System.out.println(head.printForward());
// delete node 4
deleteNode(head.next.next.next.next);
System.out.println(head.printForward());
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class Question method main.
public static void main(String[] args) {
/* Create linked list */
int[] vals = { 33, 9, 2, 3, 10, 10389, 838, 874578, 5 };
LinkedListNode head = new LinkedListNode(vals[0], null, null);
LinkedListNode current = head;
for (int i = 1; i < vals.length; i++) {
current = new LinkedListNode(vals[i], null, current);
}
System.out.println(head.printForward());
/* Partition */
LinkedListNode h = partition(head, 3);
/* Print Result */
System.out.println(h.printForward());
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class QuestionB method partition.
public static LinkedListNode partition(LinkedListNode node, int x) {
LinkedListNode beforeStart = null;
LinkedListNode afterStart = null;
/* Partition list */
while (node != null) {
LinkedListNode next = node.next;
if (node.data < x) {
/* Insert node into start of before list */
node.next = beforeStart;
beforeStart = node;
} else {
/* Insert node into front of after list */
node.next = afterStart;
afterStart = node;
}
node = next;
}
/* Merge before list and after list */
if (beforeStart == null) {
return afterStart;
}
LinkedListNode head = beforeStart;
while (beforeStart.next != null) {
beforeStart = beforeStart.next;
}
beforeStart.next = afterStart;
return head;
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class QuestionC method partition.
public static LinkedListNode partition(LinkedListNode node, int x) {
LinkedListNode head = node;
LinkedListNode tail = node;
/* Partition list */
while (node != null) {
LinkedListNode next = node.next;
if (node.data < x) {
/* Insert node at head. */
node.next = head;
head = node;
} else {
/* Insert node at tail. */
tail.next = node;
tail = node;
}
node = next;
}
tail.next = null;
return head;
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class QuestionA method main.
public static void main(String[] args) {
LinkedListNode lA1 = new LinkedListNode(9, null, null);
LinkedListNode lA2 = new LinkedListNode(9, null, lA1);
LinkedListNode lA3 = new LinkedListNode(9, null, lA2);
LinkedListNode lB1 = new LinkedListNode(1, null, null);
LinkedListNode lB2 = new LinkedListNode(0, null, lB1);
LinkedListNode lB3 = new LinkedListNode(0, null, lB2);
LinkedListNode list3 = addLists(lA1, lB1, 0);
System.out.println(" " + lA1.printForward());
System.out.println("+ " + lB1.printForward());
System.out.println("= " + list3.printForward());
int l1 = linkedListToInt(lA1);
int l2 = linkedListToInt(lB1);
int l3 = linkedListToInt(list3);
System.out.print(l1 + " + " + l2 + " = " + l3 + "\n");
System.out.print(l1 + " + " + l2 + " = " + (l1 + l2));
}
Aggregations