Skip to content

Commit 2b1f79e

Browse files
committed
Intersecting node: done
1 parent bcf3462 commit 2b1f79e

File tree

2 files changed

+116
-11
lines changed

2 files changed

+116
-11
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}

src/main/java/com/rampatra/linkedlists/IntersectionPointOfTwoLists.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*
99
* @author rampatra
1010
* @since 6/18/15
11-
* @time: 10:34 PM
1211
*/
1312
public class IntersectionPointOfTwoLists {
1413

@@ -51,17 +50,17 @@ public static <E extends Comparable<E>> SingleLinkedNode<E> getIntersectionNode(
5150
return null;
5251
}
5352

54-
public static void main(String a[]) {
53+
public static void main(String[] a) {
5554
SingleLinkedList<Integer> linkedList1 = new SingleLinkedList<>();
56-
linkedList1.add(00);
57-
linkedList1.add(11);
58-
linkedList1.add(22);
59-
linkedList1.add(33);
60-
linkedList1.add(44);
61-
linkedList1.add(55);
62-
linkedList1.add(66);
63-
linkedList1.add(77);
64-
linkedList1.add(88);
55+
linkedList1.add(0);
56+
linkedList1.add(1);
57+
linkedList1.add(2);
58+
linkedList1.add(3);
59+
linkedList1.add(4);
60+
linkedList1.add(5);
61+
linkedList1.add(6);
62+
linkedList1.add(7);
63+
linkedList1.add(8);
6564
SingleLinkedList<Integer> linkedList2 = new SingleLinkedList<>();
6665
linkedList2.add(56);
6766
linkedList2.add(78);

0 commit comments

Comments
 (0)