Skip to content

Commit 5943e38

Browse files
committed
solve 160.intersection-of-two-linked-lists
1 parent 889b924 commit 5943e38

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* @lc app=leetcode id=160 lang=java
3+
*
4+
* [160] Intersection of Two Linked Lists
5+
*
6+
* https://leetcode.com/problems/intersection-of-two-linked-lists/description/
7+
*
8+
* algorithms
9+
* Easy (35.22%)
10+
* Likes: 2346
11+
* Dislikes: 228
12+
* Total Accepted: 334.3K
13+
* Total Submissions: 949.2K
14+
* Testcase Example: '8\n[4,1,8,4,5]\n[5,0,1,8,4,5]\n2\n3'
15+
*
16+
* Write a program to find the node at which the intersection of two singly
17+
* linked lists begins.
18+
*
19+
* For example, the following two linked lists:
20+
*
21+
*
22+
* begin to intersect at node c1.
23+
*
24+
*
25+
*
26+
* Example 1:
27+
*
28+
*
29+
*
30+
* Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA =
31+
* 2, skipB = 3
32+
* Output: Reference of the node with value = 8
33+
* Input Explanation: The intersected node's value is 8 (note that this must
34+
* not be 0 if the two lists intersect). From the head of A, it reads as
35+
* [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2
36+
* nodes before the intersected node in A; There are 3 nodes before the
37+
* intersected node in B.
38+
*
39+
*
40+
*
41+
* Example 2:
42+
*
43+
*
44+
*
45+
* Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3,
46+
* skipB = 1
47+
* Output: Reference of the node with value = 2
48+
* Input Explanation: The intersected node's value is 2 (note that this must
49+
* not be 0 if the two lists intersect). From the head of A, it reads as
50+
* [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes
51+
* before the intersected node in A; There are 1 node before the intersected
52+
* node in B.
53+
*
54+
*
55+
*
56+
*
57+
* Example 3:
58+
*
59+
*
60+
*
61+
* Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB =
62+
* 2
63+
* Output: null
64+
* Input Explanation: From the head of A, it reads as [2,6,4]. From the head of
65+
* B, it reads as [1,5]. Since the two lists do not intersect, intersectVal
66+
* must be 0, while skipA and skipB can be arbitrary values.
67+
* Explanation: The two lists do not intersect, so return null.
68+
*
69+
*
70+
*
71+
*
72+
* Notes:
73+
*
74+
*
75+
* If the two linked lists have no intersection at all, return null.
76+
* The linked lists must retain their original structure after the function
77+
* returns.
78+
* You may assume there are no cycles anywhere in the entire linked
79+
* structure.
80+
* Your code should preferably run in O(n) time and use only O(1) memory.
81+
*
82+
*
83+
*/
84+
/**
85+
* Definition for singly-linked list.
86+
* public class ListNode {
87+
* int val;
88+
* ListNode next;
89+
* ListNode(int x) {
90+
* val = x;
91+
* next = null;
92+
* }
93+
* }
94+
*/
95+
public class Solution {
96+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
97+
int lenA = 0;
98+
ListNode pA = headA;
99+
for (; pA != null; pA = pA.next) {
100+
lenA++;
101+
}
102+
103+
int lenB = 0;
104+
ListNode pB = headB;
105+
for (; pB != null; pB = pB.next) {
106+
lenB++;
107+
}
108+
109+
if (pA != pB) {
110+
return null;
111+
}
112+
113+
pA = headA;
114+
pB = headB;
115+
int len = lenA - lenB;
116+
if (lenA < lenB) {
117+
pA = headB;
118+
pB = headA;
119+
len = lenB - lenA;
120+
}
121+
122+
while (len > 0) {
123+
pA = pA.next;
124+
len--;
125+
}
126+
127+
while (pA != null && pB != null) {
128+
if (pA == pB) {
129+
return pA;
130+
} else {
131+
pA = pA.next;
132+
pB = pB.next;
133+
}
134+
}
135+
136+
return null;
137+
}
138+
}
139+

0 commit comments

Comments
 (0)