Search in sources :

Example 41 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci by careercup.

the class QuestionB method main.

public static void main(String[] args) {
    int length = 20;
    LinkedListNode[] nodes = new LinkedListNode[length];
    for (int i = 0; i < length; i++) {
        nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);
    }
    for (int i = 0; i < length; i++) {
        if (i < length - 1) {
            nodes[i].setNext(nodes[i + 1]);
        }
        if (i > 0) {
            nodes[i].setPrevious(nodes[i - 1]);
        }
    }
    LinkedListNode head = nodes[0];
    System.out.println(head.printForward());
    LinkedListNode h = partition(head, 7);
    System.out.println(h.printForward());
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 42 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci 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;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 43 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci 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;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 44 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci by careercup.

the class QuestionC method main.

public static void main(String[] args) {
    int length = 20;
    LinkedListNode[] nodes = new LinkedListNode[length];
    for (int i = 0; i < length; i++) {
        nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);
    }
    for (int i = 0; i < length; i++) {
        if (i < length - 1) {
            nodes[i].setNext(nodes[i + 1]);
        }
        if (i > 0) {
            nodes[i].setPrevious(nodes[i - 1]);
        }
    }
    LinkedListNode head = nodes[0];
    System.out.println(head.printForward());
    LinkedListNode h = partition(head, 8);
    System.out.println(h.printForward());
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 45 with LinkedListNode

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

the class QuestionA method deleteDups.

public static void deleteDups(LinkedListNode n) {
    HashSet<Integer> set = new HashSet<Integer>();
    LinkedListNode previous = null;
    while (n != null) {
        if (set.contains(n.data)) {
            previous.next = n.next;
        } else {
            set.add(n.data);
            previous = n;
        }
        n = n.next;
    }
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode) HashSet(java.util.HashSet)

Aggregations

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