Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 18, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions problems/面试题02.07.链表相交.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

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

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

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

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

示例 1:
示例 1:

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

示例 2:

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

示例 3:
示例 3:

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

Expand Down Expand Up @@ -100,7 +100,7 @@ public:
## 其他语言版本


### Java
### Java
```Java
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Expand Down Expand Up @@ -144,11 +144,11 @@ public class Solution {
}
return null;
}

}
```

### Python
### Python
```python

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


while cur_a != cur_b:
cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走
cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a

return cur_a
```

### Go
### Go

```go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
Expand Down Expand Up @@ -208,7 +208,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
}
```

### javaScript
双指针

```go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
l1,l2 := headA, headB
for l1 != l2 {
if l1 != nil {
l1 = l1.Next
} else {
l1 = headB
}

if l2 != nil {
l2 = l2.Next
} else {
l2 = headA
}
}

return l1
}
```

### javaScript

```js
var getListLen = function(head) {
Expand All @@ -218,9 +241,9 @@ var getListLen = function(head) {
cur = cur.next;
}
return len;
}
}
var getIntersectionNode = function(headA, headB) {
let curA = headA,curB = headB,
let curA = headA,curB = headB,
lenA = getListLen(headA),
lenB = getListLen(headB);
if(lenA < lenB) {
Expand Down

AltStyle によって変換されたページ (->オリジナル) /