Search in sources :

Example 51 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition 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 52 with LinkedListNode

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

the class QuestionC method main.

public static void main(String[] args) {
    int length = 9;
    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]);
        }
    }
    //nodes[length - 2].data = 9; // Uncomment to ruin palindrome
    LinkedListNode head = nodes[0];
    System.out.println(head.printForward());
    System.out.println(isPalindrome(head));
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 53 with LinkedListNode

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 = { -1, -2, 0, 1, 2, 3, 4, 5, 6, 7, 8 };
    LinkedListNode list1 = AssortedMethods.createLinkedListFromArray(vals);
    int[] vals2 = { 12, 14, 15 };
    LinkedListNode list2 = AssortedMethods.createLinkedListFromArray(vals2);
    list2.next.next = list1.next.next.next.next;
    System.out.println(list1.printForward());
    System.out.println(list2.printForward());
    LinkedListNode intersection = findIntersection(list1, list2);
    System.out.println(intersection.printForward());
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 54 with LinkedListNode

use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition 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 55 with LinkedListNode

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

the class Tester method main.

public static void main(String[] args) {
    System.out.println(createLinkedList().printForward());
    /* Partition */
    LinkedListNode hA = Question.partition(createLinkedList(), 5);
    LinkedListNode hB = QuestionB.partition(createLinkedList(), 5);
    LinkedListNode hC = QuestionC.partition(createLinkedList(), 5);
    /* Print Result */
    System.out.println(hA.printForward());
    System.out.println(hB.printForward());
    System.out.println(hC.printForward());
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Aggregations

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