Skip to content

Commit 4e3f363

Browse files
committed
Update 160.Intersection-of-Two-Linked-Lists.md
1 parent 4837f28 commit 4e3f363

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

problems/160.Intersection-of-Two-Linked-Lists.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
1313
- 链表
1414
- 双指针
1515

16-
### 哈希法
16+
## 解法一: 哈希法
1717

18-
- 有 A, B 这两条链表, 先遍历其中一个,比如 A 链表, 并将 A 中的所有节点存入哈希表。
19-
- 遍历 B 链表,检查节点是否在哈希表中, 第一个存在的就是相交节点
18+
有 A, B 这两条链表, 先遍历其中一个,比如 A 链表, 并将 A 中的所有节点存入哈希表。
2019

21-
伪代码:
20+
遍历 B 链表,检查节点是否在哈希表中, 第一个存在的就是相交节点
21+
22+
- 伪代码
2223

2324
```jsx
2425
data = new Set() // 存放A链表的所有节点的地址
@@ -37,7 +38,10 @@ while B不为空{
3738
return null // 两条链表没有相交点
3839
```
3940

41+
- 代码支持: JS
42+
4043
JS Code:
44+
4145
```js
4246
let data = new Set();
4347
while (A !== null) {
@@ -56,7 +60,7 @@ return null;
5660
- 时间复杂度:$O(N)$
5761
- 空间复杂度:$O(N)$
5862

59-
### 解法二:双指针
63+
## 解法二:双指针
6064

6165
- 例如使用 a, b 两个指针分别指向 A, B 这两条链表, 两个指针相同的速度向后移动,
6266
- 当 a 到达链表的尾部时,重定位到链表 B 的头结点
@@ -72,7 +76,7 @@ return null;
7276
2. 当 a 指针将链表 1 遍历完后,重定位到链表 B 的头结点,然后继续遍历直至相交点(a 指针遍历的距离为 A + C + B)
7377
3. 同理 b 指针遍历的距离为 B + C + A
7478

75-
伪代码:
79+
- 伪代码
7680

7781
```js
7882
a = headA
@@ -90,7 +94,10 @@ while a,b指针不相等时 {
9094
return a
9195
```
9296

97+
- 代码支持: JS, Python, Go, PHP
98+
9399
JS Code:
100+
94101
```js
95102
var getIntersectionNode = function (headA, headB) {
96103
let a = headA,
@@ -104,6 +111,7 @@ var getIntersectionNode = function (headA, headB) {
104111
```
105112

106113
Python Code:
114+
107115
```py
108116
class Solution:
109117
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
@@ -115,6 +123,7 @@ class Solution:
115123
```
116124

117125
Go Code:
126+
118127
```go
119128
/**
120129
* Definition for singly-linked list.
@@ -145,6 +154,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode {
145154
```
146155

147156
PHP Code:
157+
148158
```php
149159
/**
150160
* Definition for a singly-linked list.

0 commit comments

Comments
 (0)