Search in sources :

Example 1 with LinkedListNode

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

the class QuestionB method isPalindrome.

public static boolean isPalindrome(LinkedListNode head) {
    LinkedListNode fast = head;
    LinkedListNode slow = head;
    Stack<Integer> stack = new Stack<Integer>();
    while (fast != null && fast.next != null) {
        stack.push(slow.data);
        slow = slow.next;
        fast = fast.next.next;
    }
    /* Has odd number of elements, so skip the middle */
    if (fast != null) {
        slow = slow.next;
    }
    while (slow != null) {
        int top = stack.pop().intValue();
        if (top != slow.data) {
            return false;
        }
        slow = slow.next;
    }
    return true;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode) Stack(java.util.Stack)

Example 2 with LinkedListNode

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

the class Tester method main.

public static void main(String[] args) {
    int max = 11;
    for (int length = 1; length < max; length++) {
        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]);
            }
        }
        for (int i = -1; i < length; i++) {
            if (i >= 0) {
                // Ruin palindrome
                nodes[i].data++;
            }
            LinkedListNode head = nodes[0];
            System.out.println(head.printForward());
            boolean resultA = QuestionA.isPalindrome(head);
            boolean resultB = QuestionB.isPalindrome(head);
            boolean resultC = QuestionC.isPalindrome(head);
            System.out.println("A: " + resultA);
            System.out.println("B: " + resultB);
            System.out.println("C: " + resultC);
            if (resultA != resultB || resultB != resultC) {
                System.out.println("ERROR");
                length = max;
                break;
            }
            if (i >= 0) {
                nodes[i].data--;
            }
        }
    }
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 3 with LinkedListNode

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

the class Question method getTailAndSize.

public static Result getTailAndSize(LinkedListNode list) {
    if (list == null)
        return null;
    int size = 1;
    LinkedListNode current = list;
    while (current.next != null) {
        size++;
        current = current.next;
    }
    return new Result(current, size);
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 4 with LinkedListNode

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

the class Question method getKthNode.

public static LinkedListNode getKthNode(LinkedListNode head, int k) {
    LinkedListNode current = head;
    while (k > 0 && current != null) {
        current = current.next;
        k--;
    }
    return current;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 5 with LinkedListNode

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

the class Question method findIntersection.

public static LinkedListNode findIntersection(LinkedListNode list1, LinkedListNode list2) {
    if (list1 == null || list2 == null)
        return null;
    /* Get tail and sizes. */
    Result result1 = getTailAndSize(list1);
    Result result2 = getTailAndSize(list2);
    /* If different tail nodes, then there's no intersection. */
    if (result1.tail != result2.tail) {
        return null;
    }
    /* Set pointers to the start of each linked list. */
    LinkedListNode shorter = result1.size < result2.size ? list1 : list2;
    LinkedListNode longer = result1.size < result2.size ? list2 : list1;
    /* Advance the pointer for the longer linked list by the difference in lengths. */
    longer = getKthNode(longer, Math.abs(result1.size - result2.size));
    /* Move both pointers until you have a collision. */
    while (shorter != longer) {
        shorter = shorter.next;
        longer = longer.next;
    }
    /* Return either one. */
    return longer;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Aggregations

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