Skip to content

Commit f3f7ec5

Browse files
authoredJun 2, 2022
feat: update solutions to lc problem: No.0160
No.0160.Intersection of Two Linked Lists
1 parent 7d5ddb8 commit f3f7ec5

File tree

10 files changed

+155
-171
lines changed

10 files changed

+155
-171
lines changed
 

‎solution/0100-0199/0160.Intersection of Two Linked Lists/README.md

+50-59
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@
9191

9292
<!-- 这里可写通用的实现逻辑 -->
9393

94-
使用两个指针 `cur1`, `cur2` 分别指向两个链表 `headA`, `headB`
94+
**方法一:双指针**
9595

96-
同时遍历链表,当 `cur1` 到达链表 `headA` 的末尾时,重新定位到链表 `headB` 的头节点;当 `cur2` 到达链表 `headB` 的末尾时,重新定位到链表 `headA` 的头节点
96+
使用两个指针 $a$, $b$ 分别指向两个链表 $headA$, $headB$
9797

98-
若两指针相遇,所指向的结点就是第一个公共节点。若没相遇,说明两链表无公共节点,此时两个指针都指向 `null`
98+
同时遍历链表,当 $a$ 到达链表 $headA$ 的末尾时,重新定位到链表 $headB$ 的头节点;当 $b$ 到达链表 $headB$ 的末尾时,重新定位到链表 $headA$ 的头节点。
99+
100+
若两指针相遇,所指向的结点就是第一个公共节点。若没相遇,说明两链表无公共节点,此时两个指针都指向 $null$。
101+
102+
时间复杂度 $O(m+n)$,其中 $m$ 和 $n$ 分别是链表 $headA$ 和 $headB$ 的长度。
99103

100104
<!-- tabs:start -->
101105

@@ -112,11 +116,11 @@
112116

113117
class Solution:
114118
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
115-
cur1, cur2 = headA, headB
116-
while cur1 != cur2:
117-
cur1 = headB if cur1 is None else cur1.next
118-
cur2 = headA if cur2 is None else cur2.next
119-
return cur1
119+
a, b = headA, headB
120+
while a != b:
121+
a = a.next if a else headB
122+
b = b.next if b else headA
123+
return a
120124
```
121125

122126
### **Java**
@@ -137,12 +141,12 @@ class Solution:
137141
*/
138142
public class Solution {
139143
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
140-
ListNode cur1 = headA, cur2 = headB;
141-
while (cur1 != cur2) {
142-
cur1 = cur1 == null ? headB : cur1.next;
143-
cur2 = cur2 == null ? headA : cur2.next;
144+
ListNode a = headA, b = headB;
145+
while (a != b) {
146+
a = a == null ? headB : a.next;
147+
b = b == null ? headA : b.next;
144148
}
145-
return cur1;
149+
return a;
146150
}
147151
}
148152
```
@@ -161,13 +165,13 @@ public class Solution {
161165
class Solution {
162166
public:
163167
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
164-
ListNode* cur1 = headA;
165-
ListNode* cur2 = headB;
166-
while (cur1 != cur2) {
167-
cur1 = cur1 ? cur1->next : headB;
168-
cur2 = cur2 ? cur2->next : headA;
168+
ListNode *a = headA, *b = headB;
169+
while (a != b)
170+
{
171+
a = a ? a->next : headB;
172+
b = b ? b->next : headA;
169173
}
170-
return cur1;
174+
return a;
171175
}
172176
};
173177
```
@@ -189,13 +193,13 @@ public:
189193
* @return {ListNode}
190194
*/
191195
var getIntersectionNode = function (headA, headB) {
192-
let cur1 = headA;
193-
let cur2 = headB;
194-
while (cur1 != cur2) {
195-
cur1 = cur1 ? cur1.next : headB;
196-
cur2 = cur2 ? cur2.next : headA;
196+
let a = headA;
197+
let b = headB;
198+
while (a != b) {
199+
a = a ? a.next : headB;
200+
b = b ? b.next : headA;
197201
}
198-
return cur1;
202+
return a;
199203
};
200204
```
201205

@@ -210,20 +214,20 @@ var getIntersectionNode = function (headA, headB) {
210214
* }
211215
*/
212216
func getIntersectionNode(headA, headB *ListNode) *ListNode {
213-
cur1, cur2 := headA, headB
214-
for cur1 != cur2 {
215-
if cur1 == nil {
216-
cur1 = headB
217+
a, b := headA, headB
218+
for a != b {
219+
if a == nil {
220+
a = headB
217221
} else {
218-
cur1 = cur1.Next
222+
a = a.Next
219223
}
220-
if cur2 == nil {
221-
cur2 = headA
224+
if b == nil {
225+
b = headA
222226
} else {
223-
cur2 = cur2.Next
227+
b = b.Next
224228
}
225229
}
226-
return cur1
230+
return a
227231
}
228232
```
229233

@@ -246,13 +250,13 @@ function getIntersectionNode(
246250
headA: ListNode | null,
247251
headB: ListNode | null,
248252
): ListNode | null {
249-
let p1: ListNode | null = headA;
250-
let p2: ListNode | null = headB;
251-
while (p1 != p2) {
252-
p1 = p1 == null ? headB : p1.next;
253-
p2 = p2 == null ? headA : p2.next;
253+
let a = headA;
254+
let b = headB;
255+
while (a != b) {
256+
a = a ? a.next : headB;
257+
b = b ? b.next : headA;
254258
}
255-
return p1;
259+
return a;
256260
}
257261
```
258262

@@ -273,26 +277,13 @@ function getIntersectionNode(
273277

274278
class Solution {
275279
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
276-
277-
guard let _ = headA, let _ = headB else {
278-
return nil
279-
}
280-
281-
var nodeA = headA
282-
var nodeB = headB
283-
284-
while nodeA != nodeB {
285-
nodeA = nodeA != nil ? nodeA?.next : headB
286-
nodeB = nodeB != nil ? nodeB?.next : headA
280+
var a = headA
281+
var b = headB
282+
while a !== b {
283+
a = a == nil ? headB : a?.next
284+
b = b == nil ? headA : b?.next
287285
}
288-
289-
return nodeA
290-
}
291-
}
292-
293-
extension ListNode: Equatable {
294-
public static func ==(lhs: ListNode, rhs: ListNode) -> Bool {
295-
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
286+
return a
296287
}
297288
}
298289
```

‎solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md

+43-56
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ Explanation: The two lists do not intersect, so return null.
8686

8787
class Solution:
8888
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
89-
cur1, cur2 = headA, headB
90-
while cur1 != cur2:
91-
cur1 = headB if cur1 is None else cur1.next
92-
cur2 = headA if cur2 is None else cur2.next
93-
return cur1
89+
a, b = headA, headB
90+
while a != b:
91+
a = a.next if a else headB
92+
b = b.next if b else headA
93+
return a
9494
```
9595

9696
### **Java**
@@ -109,12 +109,12 @@ class Solution:
109109
*/
110110
public class Solution {
111111
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
112-
ListNode cur1 = headA, cur2 = headB;
113-
while (cur1 != cur2) {
114-
cur1 = cur1 == null ? headB : cur1.next;
115-
cur2 = cur2 == null ? headA : cur2.next;
112+
ListNode a = headA, b = headB;
113+
while (a != b) {
114+
a = a == null ? headB : a.next;
115+
b = b == null ? headA : b.next;
116116
}
117-
return cur1;
117+
return a;
118118
}
119119
}
120120
```
@@ -133,13 +133,13 @@ public class Solution {
133133
class Solution {
134134
public:
135135
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
136-
ListNode* cur1 = headA;
137-
ListNode* cur2 = headB;
138-
while (cur1 != cur2) {
139-
cur1 = cur1 ? cur1->next : headB;
140-
cur2 = cur2 ? cur2->next : headA;
136+
ListNode *a = headA, *b = headB;
137+
while (a != b)
138+
{
139+
a = a ? a->next : headB;
140+
b = b ? b->next : headA;
141141
}
142-
return cur1;
142+
return a;
143143
}
144144
};
145145
```
@@ -161,13 +161,13 @@ public:
161161
* @return {ListNode}
162162
*/
163163
var getIntersectionNode = function (headA, headB) {
164-
let cur1 = headA;
165-
let cur2 = headB;
166-
while (cur1 != cur2) {
167-
cur1 = cur1 ? cur1.next : headB;
168-
cur2 = cur2 ? cur2.next : headA;
164+
let a = headA;
165+
let b = headB;
166+
while (a != b) {
167+
a = a ? a.next : headB;
168+
b = b ? b.next : headA;
169169
}
170-
return cur1;
170+
return a;
171171
};
172172
```
173173

@@ -182,20 +182,20 @@ var getIntersectionNode = function (headA, headB) {
182182
* }
183183
*/
184184
func getIntersectionNode(headA, headB *ListNode) *ListNode {
185-
cur1, cur2 := headA, headB
186-
for cur1 != cur2 {
187-
if cur1 == nil {
188-
cur1 = headB
185+
a, b := headA, headB
186+
for a != b {
187+
if a == nil {
188+
a = headB
189189
} else {
190-
cur1 = cur1.Next
190+
a = a.Next
191191
}
192-
if cur2 == nil {
193-
cur2 = headA
192+
if b == nil {
193+
b = headA
194194
} else {
195-
cur2 = cur2.Next
195+
b = b.Next
196196
}
197197
}
198-
return cur1
198+
return a
199199
}
200200
```
201201

@@ -218,13 +218,13 @@ function getIntersectionNode(
218218
headA: ListNode | null,
219219
headB: ListNode | null,
220220
): ListNode | null {
221-
let p1: ListNode | null = headA;
222-
let p2: ListNode | null = headB;
223-
while (p1 != p2) {
224-
p1 = p1 == null ? headB : p1.next;
225-
p2 = p2 == null ? headA : p2.next;
221+
let a = headA;
222+
let b = headB;
223+
while (a != b) {
224+
a = a ? a.next : headB;
225+
b = b ? b.next : headA;
226226
}
227-
return p1;
227+
return a;
228228
}
229229
```
230230

@@ -245,26 +245,13 @@ function getIntersectionNode(
245245

246246
class Solution {
247247
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
248-
249-
guard let _ = headA, let _ = headB else {
250-
return nil
251-
}
252-
253-
var nodeA = headA
254-
var nodeB = headB
255-
256-
while nodeA != nodeB {
257-
nodeA = nodeA != nil ? nodeA?.next : headB
258-
nodeB = nodeB != nil ? nodeB?.next : headA
248+
var a = headA
249+
var b = headB
250+
while a !== b {
251+
a = a == nil ? headB : a?.next
252+
b = b == nil ? headA : b?.next
259253
}
260-
261-
return nodeA
262-
}
263-
}
264-
265-
extension ListNode: Equatable {
266-
public static func ==(lhs: ListNode, rhs: ListNode) -> Bool {
267-
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
254+
return a
268255
}
269256
}
270257
```

‎solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
class Solution {
1010
public:
1111
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
12-
ListNode* cur1 = headA;
13-
ListNode* cur2 = headB;
14-
while (cur1 != cur2) {
15-
cur1 = cur1 ? cur1->next : headB;
16-
cur2 = cur2 ? cur2->next : headA;
12+
ListNode *a = headA, *b = headB;
13+
while (a != b)
14+
{
15+
a = a ? a->next : headB;
16+
b = b ? b->next : headA;
1717
}
18-
return cur1;
18+
return a;
1919
}
2020
};

‎solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
* }
77
*/
88
func getIntersectionNode(headA, headB *ListNode) *ListNode {
9-
cur1, cur2 := headA, headB
10-
for cur1 != cur2 {
11-
if cur1 == nil {
12-
cur1 = headB
9+
a, b := headA, headB
10+
for a != b {
11+
if a == nil {
12+
a = headB
1313
} else {
14-
cur1 = cur1.Next
14+
a = a.Next
1515
}
16-
if cur2 == nil {
17-
cur2 = headA
16+
if b == nil {
17+
b = headA
1818
} else {
19-
cur2 = cur2.Next
19+
b = b.Next
2020
}
2121
}
22-
return cur1
22+
return a
2323
}

0 commit comments

Comments
 (0)
Please sign in to comment.