Search in sources :

Example 46 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.

the class QuestionA 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);
    System.out.println(head.printForward());
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 47 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.

the class QuestionB 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);
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 48 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.

the class QuestionB method deleteDups.

public static void deleteDups(LinkedListNode head) {
    LinkedListNode current = head;
    while (current != null) {
        /* Remove all future nodes that have the same value */
        LinkedListNode runner = current;
        while (runner.next != null) {
            if (runner.next.data == current.data) {
                runner.next = runner.next.next;
            } else {
                runner = runner.next;
            }
        }
        current = current.next;
    }
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 49 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.

the class QuestionC method deleteDups.

public static void deleteDups(LinkedListNode head) {
    if (head == null)
        return;
    LinkedListNode previous = head;
    LinkedListNode current = previous.next;
    while (current != null) {
        // Look backwards for dups, and remove any that you see.
        LinkedListNode runner = head;
        while (runner != current) {
            if (runner.data == current.data) {
                LinkedListNode tmp = current.next;
                previous.next = tmp;
                current = tmp;
                /* We know we can't have more than one dup preceding
					 * our element since it would have been removed 
					 * earlier. */
                break;
            }
            runner = runner.next;
        }
        /* If runner == current, then we didn't find any duplicate 
			 * elements in the previous for loop.  We then need to 
			 * increment current.  
			 * If runner != current, then we must have hit the �break� 
			 * condition, in which case we found a dup and current has
			 * already been incremented.*/
        if (runner == current) {
            previous = current;
            current = current.next;
        }
    }
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 50 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.

the class Question method partition.

public static LinkedListNode partition(LinkedListNode node, int x) {
    LinkedListNode beforeStart = null;
    LinkedListNode beforeEnd = null;
    LinkedListNode afterStart = null;
    LinkedListNode afterEnd = null;
    /* Partition list */
    while (node != null) {
        LinkedListNode next = node.next;
        node.next = null;
        if (node.data < x) {
            if (beforeStart == null) {
                beforeStart = node;
                beforeEnd = beforeStart;
            } else {
                beforeEnd.next = node;
                beforeEnd = node;
            }
        } else {
            if (afterStart == null) {
                afterStart = node;
                afterEnd = afterStart;
            } else {
                afterEnd.next = node;
                afterEnd = node;
            }
        }
        node = next;
    }
    /* Merge before list and after list */
    if (beforeStart == null) {
        return afterStart;
    }
    beforeEnd.next = afterStart;
    return beforeStart;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Aggregations

LinkedListNode (CtCILibrary.LinkedListNode)59 HashSet (java.util.HashSet)2 Stack (java.util.Stack)2