Skip to content

Commit 04fb43d

Browse files
committed
feat: add solutions to lc/lcci problem: Intersection of Two Linked Lists
1 parent 2ba31fe commit 04fb43d

File tree

14 files changed

+525
-193
lines changed

14 files changed

+525
-193
lines changed

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

+91-42
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
<!-- 这里可写通用的实现逻辑 -->
1313

14-
先求两链表的长度差 `differ`,接着较长的链表先走 `differ` 步,之后两链表同时走,若相遇,则说明相交。
14+
使用两个指针 `cur1`, `cur2` 分别指向两个链表 `headA`, `headB`
15+
16+
同时遍历链表,当 `cur1` 到达链表 `headA` 的末尾时,重新定位到链表 `headB` 的头节点;当 `cur2` 到达链表 `headB` 的末尾时,重新定位到链表 `headA` 的头节点。
17+
18+
若两指针相遇,所指向的结点就是第一个公共节点。若没相遇,说明两链表无公共节点,此时两个指针都指向 `null`
1519

1620
<!-- tabs:start -->
1721

@@ -28,25 +32,11 @@
2832

2933
class Solution:
3034
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
31-
len1, len2 = self._length(headA), self._length(headB)
32-
if len1 < len2:
33-
headA, headB = headB, headA
34-
differ = abs(len1 - len2)
35-
for _ in range(differ):
36-
headA = headA.next
37-
while headA:
38-
if headA == headB:
39-
return headA
40-
headA = headA.next
41-
headB = headB.next
42-
return None
43-
44-
def _length(self, node: ListNode) -> int:
45-
n = 0
46-
while node:
47-
node = node.next
48-
n += 1
49-
return n
35+
cur1, cur2 = headA, headB
36+
while cur1 != cur2:
37+
cur1 = headB if cur1 is None else cur1.next
38+
cur2 = headA if cur2 is None else cur2.next
39+
return cur1
5040
```
5141

5242
### **Java**
@@ -67,34 +57,93 @@ class Solution:
6757
*/
6858
public class Solution {
6959
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
70-
int len1 = len(headA), len2 = len(headB);
71-
int differ = Math.abs(len1 - len2);
72-
if (len1 < len2) {
73-
ListNode t = headA;
74-
headA = headB;
75-
headB = t;
60+
ListNode cur1 = headA, cur2 = headB;
61+
while (cur1 != cur2) {
62+
cur1 = cur1 == null ? headB : cur1.next;
63+
cur2 = cur2 == null ? headA : cur2.next;
7664
}
77-
while (differ-- > 0) {
78-
headA = headA.next;
79-
}
80-
while (headA != null) {
81-
if (headA == headB) {
82-
return headA;
83-
}
84-
headA = headA.next;
85-
headB = headB.next;
65+
return cur1;
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
/**
74+
* Definition for singly-linked list.
75+
* struct ListNode {
76+
* int val;
77+
* ListNode *next;
78+
* ListNode(int x) : val(x), next(NULL) {}
79+
* };
80+
*/
81+
class Solution {
82+
public:
83+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
84+
ListNode* cur1 = headA;
85+
ListNode* cur2 = headB;
86+
while (cur1 != cur2) {
87+
cur1 = cur1 ? cur1->next : headB;
88+
cur2 = cur2 ? cur2->next : headA;
8689
}
87-
return null;
90+
return cur1;
8891
}
92+
};
93+
```
94+
95+
### **JavaScript**
8996
90-
private int len(ListNode node) {
91-
int n = 0;
92-
while (node != null) {
93-
node = node.next;
94-
++n;
97+
```js
98+
/**
99+
* Definition for singly-linked list.
100+
* function ListNode(val) {
101+
* this.val = val;
102+
* this.next = null;
103+
* }
104+
*/
105+
106+
/**
107+
* @param {ListNode} headA
108+
* @param {ListNode} headB
109+
* @return {ListNode}
110+
*/
111+
var getIntersectionNode = function(headA, headB) {
112+
let cur1 = headA;
113+
let cur2 = headB;
114+
while (cur1 != cur2) {
115+
cur1 = cur1 ? cur1.next : headB;
116+
cur2 = cur2 ? cur2.next : headA;
117+
}
118+
return cur1;
119+
};
120+
```
121+
122+
### **Go**
123+
124+
```go
125+
/**
126+
* Definition for singly-linked list.
127+
* type ListNode struct {
128+
* Val int
129+
* Next *ListNode
130+
* }
131+
*/
132+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
133+
cur1, cur2 := headA, headB
134+
for cur1 != cur2 {
135+
if cur1 == nil {
136+
cur1 = headB
137+
} else {
138+
cur1 = cur1.Next
139+
}
140+
if cur2 == nil {
141+
cur2 = headA
142+
} else {
143+
cur2 = cur2.Next
95144
}
96-
return n;
97145
}
146+
return cur1
98147
}
99148
```
100149

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

+87-42
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,11 @@
6060

6161
class Solution:
6262
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
63-
len1, len2 = self._length(headA), self._length(headB)
64-
if len1 < len2:
65-
headA, headB = headB, headA
66-
differ = abs(len1 - len2)
67-
for _ in range(differ):
68-
headA = headA.next
69-
while headA:
70-
if headA == headB:
71-
return headA
72-
headA = headA.next
73-
headB = headB.next
74-
return None
75-
76-
def _length(self, node: ListNode) -> int:
77-
n = 0
78-
while node:
79-
node = node.next
80-
n += 1
81-
return n
63+
cur1, cur2 = headA, headB
64+
while cur1 != cur2:
65+
cur1 = headB if cur1 is None else cur1.next
66+
cur2 = headA if cur2 is None else cur2.next
67+
return cur1
8268
```
8369

8470
### **Java**
@@ -97,34 +83,93 @@ class Solution:
9783
*/
9884
public class Solution {
9985
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
100-
int len1 = len(headA), len2 = len(headB);
101-
int differ = Math.abs(len1 - len2);
102-
if (len1 < len2) {
103-
ListNode t = headA;
104-
headA = headB;
105-
headB = t;
86+
ListNode cur1 = headA, cur2 = headB;
87+
while (cur1 != cur2) {
88+
cur1 = cur1 == null ? headB : cur1.next;
89+
cur2 = cur2 == null ? headA : cur2.next;
10690
}
107-
while (differ-- > 0) {
108-
headA = headA.next;
109-
}
110-
while (headA != null) {
111-
if (headA == headB) {
112-
return headA;
113-
}
114-
headA = headA.next;
115-
headB = headB.next;
91+
return cur1;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
/**
100+
* Definition for singly-linked list.
101+
* struct ListNode {
102+
* int val;
103+
* ListNode *next;
104+
* ListNode(int x) : val(x), next(NULL) {}
105+
* };
106+
*/
107+
class Solution {
108+
public:
109+
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
110+
ListNode* cur1 = headA;
111+
ListNode* cur2 = headB;
112+
while (cur1 != cur2) {
113+
cur1 = cur1 ? cur1->next : headB;
114+
cur2 = cur2 ? cur2->next : headA;
116115
}
117-
return null;
116+
return cur1;
118117
}
118+
};
119+
```
119120
120-
private int len(ListNode node) {
121-
int n = 0;
122-
while (node != null) {
123-
node = node.next;
124-
++n;
121+
### **JavaScript**
122+
123+
```js
124+
/**
125+
* Definition for singly-linked list.
126+
* function ListNode(val) {
127+
* this.val = val;
128+
* this.next = null;
129+
* }
130+
*/
131+
132+
/**
133+
* @param {ListNode} headA
134+
* @param {ListNode} headB
135+
* @return {ListNode}
136+
*/
137+
var getIntersectionNode = function(headA, headB) {
138+
let cur1 = headA;
139+
let cur2 = headB;
140+
while (cur1 != cur2) {
141+
cur1 = cur1 ? cur1.next : headB;
142+
cur2 = cur2 ? cur2.next : headA;
143+
}
144+
return cur1;
145+
};
146+
```
147+
148+
### **Go**
149+
150+
```go
151+
/**
152+
* Definition for singly-linked list.
153+
* type ListNode struct {
154+
* Val int
155+
* Next *ListNode
156+
* }
157+
*/
158+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
159+
cur1, cur2 := headA, headB
160+
for cur1 != cur2 {
161+
if cur1 == nil {
162+
cur1 = headB
163+
} else {
164+
cur1 = cur1.Next
165+
}
166+
if cur2 == nil {
167+
cur2 = headA
168+
} else {
169+
cur2 = cur2.Next
125170
}
126-
return n;
127171
}
172+
return cur1
128173
}
129174
```
130175

@@ -134,4 +179,4 @@ public class Solution {
134179
135180
```
136181

137-
<!-- tabs:end -->
182+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
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;
17+
}
18+
return cur1;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
9+
cur1, cur2 := headA, headB
10+
for cur1 != cur2 {
11+
if cur1 == nil {
12+
cur1 = headB
13+
} else {
14+
cur1 = cur1.Next
15+
}
16+
if cur2 == nil {
17+
cur2 = headA
18+
} else {
19+
cur2 = cur2.Next
20+
}
21+
}
22+
return cur1
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14+
ListNode cur1 = headA, cur2 = headB;
15+
while (cur1 != cur2) {
16+
cur1 = cur1 == null ? headB : cur1.next;
17+
cur2 = cur2 == null ? headA : cur2.next;
18+
}
19+
return cur1;
20+
}
21+
}

0 commit comments

Comments
 (0)