Skip to content

Commit 1f10d17

Browse files
authored
feat: add solutions to lcci problem: No.02.07 (#2529)
No.02.07.Intersection of Two Linked Lists
1 parent b7d690d commit 1f10d17

File tree

11 files changed

+264
-99
lines changed

11 files changed

+264
-99
lines changed

lcci/02.07.Intersection of Two Linked Lists/README.md

+89-32
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99

1010
## 解法
1111

12-
### 方法一
12+
### 方法一:双指针
13+
14+
我们使用两个指针 $a$, $b$ 分别指向两个链表 $headA$, $headB$。
15+
16+
同时遍历链表,当 $a$ 到达链表 $headA$ 的末尾时,重新定位到链表 $headB$ 的头节点;当 $b$ 到达链表 $headB$ 的末尾时,重新定位到链表 $headA$ 的头节点。
17+
18+
若两指针相遇,所指向的结点就是第一个公共节点。若没相遇,说明两链表无公共节点,此时两个指针都指向 `null`,返回其中一个即可。
19+
20+
时间复杂度 $O(m+n)$,其中 $m$ 和 $n$ 分别是链表 $headA$ 和 $headB$ 的长度。空间复杂度 $O(1)$。
1321

1422
<!-- tabs:start -->
1523

@@ -23,11 +31,11 @@
2331

2432
class Solution:
2533
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
26-
cur1, cur2 = headA, headB
27-
while cur1 != cur2:
28-
cur1 = headB if cur1 is None else cur1.next
29-
cur2 = headA if cur2 is None else cur2.next
30-
return cur1
34+
a, b = headA, headB
35+
while a != b:
36+
a = a.next if a else headB
37+
b = b.next if b else headA
38+
return a
3139
```
3240

3341
```java
@@ -44,12 +52,12 @@ class Solution:
4452
*/
4553
public class Solution {
4654
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
47-
ListNode cur1 = headA, cur2 = headB;
48-
while (cur1 != cur2) {
49-
cur1 = cur1 == null ? headB : cur1.next;
50-
cur2 = cur2 == null ? headA : cur2.next;
55+
ListNode a = headA, b = headB;
56+
while (a != b) {
57+
a = a == null ? headB : a.next;
58+
b = b == null ? headA : b.next;
5159
}
52-
return cur1;
60+
return a;
5361
}
5462
}
5563
```
@@ -66,13 +74,12 @@ public class Solution {
6674
class Solution {
6775
public:
6876
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
69-
ListNode* cur1 = headA;
70-
ListNode* cur2 = headB;
71-
while (cur1 != cur2) {
72-
cur1 = cur1 ? cur1->next : headB;
73-
cur2 = cur2 ? cur2->next : headA;
77+
ListNode *a = headA, *b = headB;
78+
while (a != b) {
79+
a = a ? a->next : headB;
80+
b = b ? b->next : headA;
7481
}
75-
return cur1;
82+
return a;
7683
}
7784
};
7885
```
@@ -86,20 +93,44 @@ public:
8693
* }
8794
*/
8895
func getIntersectionNode(headA, headB *ListNode) *ListNode {
89-
cur1, cur2 := headA, headB
90-
for cur1 != cur2 {
91-
if cur1 == nil {
92-
cur1 = headB
96+
a, b := headA, headB
97+
for a != b {
98+
if a == nil {
99+
a = headB
93100
} else {
94-
cur1 = cur1.Next
101+
a = a.Next
95102
}
96-
if cur2 == nil {
97-
cur2 = headA
103+
if b == nil {
104+
b = headA
98105
} else {
99-
cur2 = cur2.Next
106+
b = b.Next
100107
}
101108
}
102-
return cur1
109+
return a
110+
}
111+
```
112+
113+
```ts
114+
/**
115+
* Definition for singly-linked list.
116+
* class ListNode {
117+
* val: number
118+
* next: ListNode | null
119+
* constructor(val?: number, next?: ListNode | null) {
120+
* this.val = (val===undefined ? 0 : val)
121+
* this.next = (next===undefined ? null : next)
122+
* }
123+
* }
124+
*/
125+
126+
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
127+
let a = headA;
128+
let b = headB;
129+
while (a != b) {
130+
a = a ? a.next : headB;
131+
b = b ? b.next : headA;
132+
}
133+
return a;
103134
}
104135
```
105136

@@ -118,16 +149,42 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
118149
* @return {ListNode}
119150
*/
120151
var getIntersectionNode = function (headA, headB) {
121-
let cur1 = headA;
122-
let cur2 = headB;
123-
while (cur1 != cur2) {
124-
cur1 = cur1 ? cur1.next : headB;
125-
cur2 = cur2 ? cur2.next : headA;
152+
let a = headA;
153+
let b = headB;
154+
while (a != b) {
155+
a = a ? a.next : headB;
156+
b = b ? b.next : headA;
126157
}
127-
return cur1;
158+
return a;
128159
};
129160
```
130161

162+
```swift
163+
/**
164+
* Definition for singly-linked list.
165+
* public class ListNode {
166+
* public var val: Int
167+
* public var next: ListNode?
168+
* public init(_ val: Int) {
169+
* self.val = val
170+
* self.next = nil
171+
* }
172+
* }
173+
*/
174+
175+
class Solution {
176+
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
177+
var a = headA
178+
var b = headB
179+
while a !== b {
180+
a = a == nil ? headB : a?.next
181+
b = b == nil ? headA : b?.next
182+
}
183+
return a
184+
}
185+
}
186+
```
187+
131188
<!-- tabs:end -->
132189

133190
<!-- end -->

lcci/02.07.Intersection of Two Linked Lists/README_EN.md

+89-32
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Two Pointers
51+
52+
We use two pointers $a$ and $b$ to point to two linked lists $headA$ and $headB$ respectively.
53+
54+
We traverse the linked lists simultaneously. When $a$ reaches the end of the linked list $headA$, it is repositioned to the head node of the linked list $headB$. When $b$ reaches the end of the linked list $headB$, it is repositioned to the head node of the linked list $headA$.
55+
56+
If the two pointers meet, the node they point to is the first common node. If they don't meet, it means that the two linked lists have no common nodes. At this time, both pointers point to `null`, and we can return either one.
57+
58+
The time complexity is $O(m+n)$, where $m$ and $n$ are the lengths of the linked lists $headA$ and $headB$ respectively. The space complexity is $O(1)$.
5159

5260
<!-- tabs:start -->
5361

@@ -61,11 +69,11 @@
6169

6270
class Solution:
6371
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
64-
cur1, cur2 = headA, headB
65-
while cur1 != cur2:
66-
cur1 = headB if cur1 is None else cur1.next
67-
cur2 = headA if cur2 is None else cur2.next
68-
return cur1
72+
a, b = headA, headB
73+
while a != b:
74+
a = a.next if a else headB
75+
b = b.next if b else headA
76+
return a
6977
```
7078

7179
```java
@@ -82,12 +90,12 @@ class Solution:
8290
*/
8391
public class Solution {
8492
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
85-
ListNode cur1 = headA, cur2 = headB;
86-
while (cur1 != cur2) {
87-
cur1 = cur1 == null ? headB : cur1.next;
88-
cur2 = cur2 == null ? headA : cur2.next;
93+
ListNode a = headA, b = headB;
94+
while (a != b) {
95+
a = a == null ? headB : a.next;
96+
b = b == null ? headA : b.next;
8997
}
90-
return cur1;
98+
return a;
9199
}
92100
}
93101
```
@@ -104,13 +112,12 @@ public class Solution {
104112
class Solution {
105113
public:
106114
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
107-
ListNode* cur1 = headA;
108-
ListNode* cur2 = headB;
109-
while (cur1 != cur2) {
110-
cur1 = cur1 ? cur1->next : headB;
111-
cur2 = cur2 ? cur2->next : headA;
115+
ListNode *a = headA, *b = headB;
116+
while (a != b) {
117+
a = a ? a->next : headB;
118+
b = b ? b->next : headA;
112119
}
113-
return cur1;
120+
return a;
114121
}
115122
};
116123
```
@@ -124,20 +131,44 @@ public:
124131
* }
125132
*/
126133
func getIntersectionNode(headA, headB *ListNode) *ListNode {
127-
cur1, cur2 := headA, headB
128-
for cur1 != cur2 {
129-
if cur1 == nil {
130-
cur1 = headB
134+
a, b := headA, headB
135+
for a != b {
136+
if a == nil {
137+
a = headB
131138
} else {
132-
cur1 = cur1.Next
139+
a = a.Next
133140
}
134-
if cur2 == nil {
135-
cur2 = headA
141+
if b == nil {
142+
b = headA
136143
} else {
137-
cur2 = cur2.Next
144+
b = b.Next
138145
}
139146
}
140-
return cur1
147+
return a
148+
}
149+
```
150+
151+
```ts
152+
/**
153+
* Definition for singly-linked list.
154+
* class ListNode {
155+
* val: number
156+
* next: ListNode | null
157+
* constructor(val?: number, next?: ListNode | null) {
158+
* this.val = (val===undefined ? 0 : val)
159+
* this.next = (next===undefined ? null : next)
160+
* }
161+
* }
162+
*/
163+
164+
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
165+
let a = headA;
166+
let b = headB;
167+
while (a != b) {
168+
a = a ? a.next : headB;
169+
b = b ? b.next : headA;
170+
}
171+
return a;
141172
}
142173
```
143174

@@ -156,16 +187,42 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
156187
* @return {ListNode}
157188
*/
158189
var getIntersectionNode = function (headA, headB) {
159-
let cur1 = headA;
160-
let cur2 = headB;
161-
while (cur1 != cur2) {
162-
cur1 = cur1 ? cur1.next : headB;
163-
cur2 = cur2 ? cur2.next : headA;
190+
let a = headA;
191+
let b = headB;
192+
while (a != b) {
193+
a = a ? a.next : headB;
194+
b = b ? b.next : headA;
164195
}
165-
return cur1;
196+
return a;
166197
};
167198
```
168199

200+
```swift
201+
/**
202+
* Definition for singly-linked list.
203+
* public class ListNode {
204+
* public var val: Int
205+
* public var next: ListNode?
206+
* public init(_ val: Int) {
207+
* self.val = val
208+
* self.next = nil
209+
* }
210+
* }
211+
*/
212+
213+
class Solution {
214+
func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
215+
var a = headA
216+
var b = headB
217+
while a !== b {
218+
a = a == nil ? headB : a?.next
219+
b = b == nil ? headA : b?.next
220+
}
221+
return a
222+
}
223+
}
224+
```
225+
169226
<!-- tabs:end -->
170227

171228
<!-- end -->

lcci/02.07.Intersection of Two Linked Lists/Solution.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
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+
a = a ? a->next : headB;
15+
b = b ? b->next : headA;
1716
}
18-
return cur1;
17+
return a;
1918
}
2019
};

lcci/02.07.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)