|
| 1 | +package com.ctci.linkedlists; |
| 2 | + |
| 3 | +import static com.ctci.linkedlists.Node.printList; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author rampatra |
| 7 | + * @since 2019-02-02 |
| 8 | + */ |
| 9 | +public class Intersection { |
| 10 | + |
| 11 | + /** |
| 12 | + * Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that |
| 13 | + * the intersection is defined based on reference, not value. That is, if the kth node of the first linked list is |
| 14 | + * the exact same node (by reference) as the jth node of the second linked list, then they are intersecting. |
| 15 | + * |
| 16 | + * @param n1 |
| 17 | + * @param n2 |
| 18 | + * @return the intersecting node, {@code null} otherwise. |
| 19 | + */ |
| 20 | + private static Node findIntersectingNode(Node n1, Node n2) { |
| 21 | + Node curr1 = n1; |
| 22 | + Node curr2 = n2; |
| 23 | + int len1 = 1; |
| 24 | + int len2 = 1; |
| 25 | + |
| 26 | + /* Calculate the length of both linked lists */ |
| 27 | + while (curr1 != null && curr1.next != null) { |
| 28 | + len1++; |
| 29 | + curr1 = curr1.next; |
| 30 | + } |
| 31 | + while (curr2 != null && curr2.next != null) { |
| 32 | + len2++; |
| 33 | + curr2 = curr2.next; |
| 34 | + } |
| 35 | + |
| 36 | + // if different tail nodes, they don't intersect |
| 37 | + if (curr1 != curr2) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + curr1 = n1; |
| 42 | + curr2 = n2; |
| 43 | + |
| 44 | + /* Get rid of the extra nodes from the longer list */ |
| 45 | + if (len1 > len2) { |
| 46 | + for (int i = 0; i < len1 - len2; i++) { |
| 47 | + curr1 = curr1.next; |
| 48 | + } |
| 49 | + } |
| 50 | + if (len2 > len1) { |
| 51 | + for (int i = 0; i < len2 - len1; i++) { |
| 52 | + curr2 = curr2.next; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // move both pointers until you have a collision |
| 57 | + while (curr1 != curr2) { |
| 58 | + curr1 = curr1.next; |
| 59 | + curr2 = curr2.next; |
| 60 | + } |
| 61 | + |
| 62 | + // return either |
| 63 | + return curr1; |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) { |
| 67 | + Node l1 = new Node(1); |
| 68 | + l1.next = new Node(2); |
| 69 | + l1.next.next = new Node(3); |
| 70 | + l1.next.next.next = new Node(4); |
| 71 | + l1.next.next.next.next = new Node(5); |
| 72 | + l1.next.next.next.next.next = new Node(5); |
| 73 | + Node l2 = new Node(1); |
| 74 | + l2.next = new Node(4); |
| 75 | + l2.next.next = new Node(2); |
| 76 | + l2.next.next.next = new Node(3); |
| 77 | + l2.next.next.next.next = l1.next.next.next; |
| 78 | + printList(l1); |
| 79 | + printList(l2); |
| 80 | + System.out.println(findIntersectingNode(l1, l2).val); // may throw NPE, not handling for the sake of simplicity |
| 81 | + |
| 82 | + System.out.println("----"); |
| 83 | + |
| 84 | + l1 = new Node(1); |
| 85 | + l2 = l1; |
| 86 | + printList(l1); |
| 87 | + printList(l2); |
| 88 | + System.out.println(findIntersectingNode(l1, l2).val); // may throw NPE, not handling for the sake of simplicity |
| 89 | + |
| 90 | + System.out.println("----"); |
| 91 | + |
| 92 | + l1 = new Node(1); |
| 93 | + l1.next = new Node(2); |
| 94 | + l1.next.next = new Node(3); |
| 95 | + l1.next.next.next = new Node(4); |
| 96 | + l1.next.next.next.next = new Node(5); |
| 97 | + l1.next.next.next.next.next = new Node(5); |
| 98 | + l2 = new Node(1); |
| 99 | + l2.next = new Node(4); |
| 100 | + l2.next.next = new Node(2); |
| 101 | + l2.next.next.next = new Node(3); |
| 102 | + printList(l1); |
| 103 | + printList(l2); |
| 104 | + System.out.println(findIntersectingNode(l1, l2)); |
| 105 | + } |
| 106 | +} |
0 commit comments