Skip to content

Commit e76c7e1

Browse files
refactor 160
1 parent f3fe755 commit e76c7e1

File tree

1 file changed

+45
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+45
-0
lines changed

src/main/java/com/fishercoder/solutions/_160.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,49 @@
1919
B: b1 → b2 → b3
2020
begin to intersect at node c1.
2121
22+
Example 1:
23+
A: 4 → 1
24+
25+
8 → 4 → 5
26+
27+
B: 5 → 0 → 1
28+
29+
30+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
31+
Output: Reference of the node with value = 8
32+
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
33+
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5].
34+
There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
35+
36+
37+
Example 2:
38+
A: 0 -> 9 → 1
39+
40+
2 → 4
41+
42+
B: 3
43+
44+
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
45+
Output: Reference of the node with value = 2
46+
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
47+
From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4].
48+
There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
49+
50+
51+
Example 3:
52+
A: 2 → 6 -> 4
53+
54+
B: 1 -> 5
55+
56+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
57+
Output: null
58+
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5].
59+
Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
60+
Explanation: The two lists do not intersect, so return null.
61+
2262
2363
Notes:
64+
2465
If the two linked lists have no intersection at all, return null.
2566
The linked lists must retain their original structure after the function returns.
2667
You may assume there are no cycles anywhere in the entire linked structure.
@@ -66,6 +107,8 @@ private int findLen(ListNode head) {
66107

67108
public static class Solution2 {
68109
/**
110+
* Most optimal solution:
111+
*
69112
* O(m+n) time
70113
* O(1) space
71114
* credit: https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len*/
@@ -77,7 +120,9 @@ public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
77120
ListNode a = headA;
78121
ListNode b = headB;
79122

123+
/**if a and b have different lengths, then it will stop the loop after second iteration*/
80124
while (a != b) {
125+
/**for the first iteration, it'll just reset the pointer to the head of another linkedlist*/
81126
a = a == null ? headB : a.next;
82127
b = b == null ? headA : b.next;
83128
}

0 commit comments

Comments
 (0)