Skip to content

Commit 7d3a585

Browse files
Merge pull request youngyangyang04#1457 from LIU-HONGYANG/hongyangliu.add-solution
refactor: add golang solution to Intersection of Two Linked Lists LCCI
2 parents 00c8774 + 26f5e59 commit 7d3a585

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

problems/面试题02.07.链表相交.md

+38-15
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313

1414
图示两个链表在节点 c1 开始相交:
1515

16-
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png)
16+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png)
1717

1818
题目数据 保证 整个链式结构中不存在环。
1919

20-
注意,函数返回结果后,链表必须 保持其原始结构 。
20+
注意,函数返回结果后,链表必须 保持其原始结构 。
2121

22-
示例 1:
22+
示例 1:
2323

24-
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png)
24+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png)
2525

2626
示例 2:
2727

28-
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png)
28+
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png)
2929

30-
示例 3:
30+
示例 3:
3131

3232
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)
3333

@@ -100,7 +100,7 @@ public:
100100
## 其他语言版本
101101
102102
103-
### Java
103+
### Java
104104
```Java
105105
public class Solution {
106106
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
@@ -144,11 +144,11 @@ public class Solution {
144144
}
145145
return null;
146146
}
147-
147+
148148
}
149149
```
150150

151-
### Python
151+
### Python
152152
```python
153153

154154
class Solution:
@@ -162,15 +162,15 @@ class Solution:
162162
"""
163163
cur_a, cur_b = headA, headB # 用两个指针代替a和b
164164

165-
165+
166166
while cur_a != cur_b:
167167
cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走
168168
cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a
169-
169+
170170
return cur_a
171171
```
172172

173-
### Go
173+
### Go
174174

175175
```go
176176
func getIntersectionNode(headA, headB *ListNode) *ListNode {
@@ -208,7 +208,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
208208
}
209209
```
210210

211-
### javaScript
211+
双指针
212+
213+
```go
214+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
215+
l1,l2 := headA, headB
216+
for l1 != l2 {
217+
if l1 != nil {
218+
l1 = l1.Next
219+
} else {
220+
l1 = headB
221+
}
222+
223+
if l2 != nil {
224+
l2 = l2.Next
225+
} else {
226+
l2 = headA
227+
}
228+
}
229+
230+
return l1
231+
}
232+
```
233+
234+
### javaScript
212235

213236
```js
214237
var getListLen = function(head) {
@@ -218,9 +241,9 @@ var getListLen = function(head) {
218241
cur = cur.next;
219242
}
220243
return len;
221-
}
244+
}
222245
var getIntersectionNode = function(headA, headB) {
223-
let curA = headA,curB = headB,
246+
let curA = headA,curB = headB,
224247
lenA = getListLen(headA),
225248
lenB = getListLen(headB);
226249
if(lenA < lenB) {

0 commit comments

Comments
 (0)