use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class Question method main.
public static void main(String[] args) {
int list_length = 100;
int k = 10;
// Create linked list
LinkedListNode[] nodes = new LinkedListNode[list_length];
for (int i = 0; i < list_length; i++) {
nodes[i] = new LinkedListNode(i, null, i > 0 ? nodes[i - 1] : null);
}
// Create loop;
nodes[list_length - 1].next = nodes[list_length - k];
LinkedListNode loop = FindBeginning(nodes[0]);
if (loop == null) {
System.out.println("No Cycle.");
} else {
System.out.println(loop.data);
}
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class Question method FindBeginning.
public static LinkedListNode FindBeginning(LinkedListNode head) {
LinkedListNode slow = head;
LinkedListNode fast = head;
// Find meeting point
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
// Error check - there is no meeting point, and therefore no loop
if (fast == null || fast.next == null) {
return null;
}
/* Move slow to Head. Keep fast at Meeting Point. Each are k steps
/* from the Loop Start. If they move at the same pace, they must
* meet at Loop Start. */
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
// Both now point to the start of the loop.
return fast;
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class QuestionC method main.
public static void main(String[] args) {
//AssortedMethods.randomLinkedList(1000, 0, 2);
LinkedListNode first = new LinkedListNode(0, null, null);
LinkedListNode head = first;
LinkedListNode second = first;
for (int i = 1; i < 8; i++) {
second = new LinkedListNode(i % 2, null, null);
first.setNext(second);
second.setPrevious(first);
first = second;
}
System.out.println(head.printForward());
deleteDups(head);
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class Tester method main.
public static void main(String[] args) {
//AssortedMethods.randomLinkedList(1000, 0, 2);
LinkedListNode first = new LinkedListNode(0, null, null);
LinkedListNode head = first;
LinkedListNode second = first;
for (int i = 1; i < 8; i++) {
second = new LinkedListNode(i % 2, null, null);
first.setNext(second);
second.setPrevious(first);
first = second;
}
System.out.println(head.printForward());
LinkedListNode cloneA = head.clone();
LinkedListNode cloneB = head.clone();
LinkedListNode cloneC = head.clone();
QuestionA.deleteDups(cloneA);
QuestionA.deleteDups(cloneB);
QuestionA.deleteDups(cloneC);
System.out.println(cloneA.printForward());
System.out.println(cloneB.printForward());
System.out.println(cloneC.printForward());
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class Question method deleteNode.
public static boolean deleteNode(LinkedListNode n) {
if (n == null || n.next == null) {
// Failure
return false;
}
LinkedListNode next = n.next;
n.data = next.data;
n.next = next.next;
return true;
}
Aggregations