Search in sources :

Example 31 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci by careercup.

the class QuestionB method main.

public static void main(String[] args) {
    LinkedListNode lA1 = new LinkedListNode(3, null, null);
    LinkedListNode lA2 = new LinkedListNode(1, null, lA1);
    LinkedListNode lA3 = new LinkedListNode(5, null, lA2);
    LinkedListNode lB1 = new LinkedListNode(5, null, null);
    LinkedListNode lB2 = new LinkedListNode(9, null, lB1);
    LinkedListNode lB3 = new LinkedListNode(1, null, lB2);
    LinkedListNode list3 = addLists(lA1, lB1);
    System.out.println("  " + lA1.printForward());
    System.out.println("+ " + lB1.printForward());
    System.out.println("= " + list3.printForward());
    int l1 = linkedListToInt(lA1);
    int l2 = linkedListToInt(lB1);
    int l3 = linkedListToInt(list3);
    System.out.print(l1 + " + " + l2 + " = " + l3 + "\n");
    System.out.print(l1 + " + " + l2 + " = " + (l1 + l2));
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 32 with LinkedListNode

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

Example 33 with LinkedListNode

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

Example 34 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci by careercup.

the class Question method main.

public static void main(String[] args) {
    int length = 10;
    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());
    Question q = new Question();
    System.out.println(q.isPalindrome(head));
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Example 35 with LinkedListNode

use of CtCILibrary.LinkedListNode in project ctci by careercup.

the class Question method isPalindrome.

public boolean isPalindrome(LinkedListNode head) {
    int size = 0;
    LinkedListNode n = head;
    while (n != null) {
        size++;
        n = n.next;
    }
    Result p = isPalindromeRecurse(head, size);
    return p.result;
}
Also used : LinkedListNode(CtCILibrary.LinkedListNode)

Aggregations

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