Skip to content

Commit ff743e2

Browse files
committed
160-intersection-of-two-linked-lists.md Separated Chinese and English solutions.
1 parent 079bb7d commit ff743e2

File tree

2 files changed

+46
-172
lines changed

2 files changed

+46
-172
lines changed

en/1-1000/160-intersection-of-two-linked-lists.md

Lines changed: 9 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# 160. Intersection of Two Linked Lists - Best Practices of LeetCode Solutions
2-
LeetCode link: [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists),
3-
[160. 相交链表](https://leetcode.cn/problems/intersection-of-two-linked-lists)
2+
LeetCode link: [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists), difficulty: **Easy**.
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
4+
## Description of "160. Intersection of Two Linked Lists"
85
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`.
96

107
For example, the following two linked lists begin to intersect at node `c1`:
@@ -15,8 +12,6 @@ The test cases are generated such that there are **no cycles** anywhere in the e
1512

1613
**Note** that the linked lists must **retain their original structure** after the function returns.
1714

18-
Difficulty: **Easy**
19-
2015
### [Example 1]
2116
![](../../images/examples/160_1.png)
2217

@@ -25,6 +20,13 @@ Difficulty: **Easy**
2520
**Output**: `Intersected at '8'`
2621

2722
### [Example 2]
23+
![](../../images/examples/160_2.png)
24+
25+
**Input**: `intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4]`
26+
27+
**Output**: `Intersected at '2'`
28+
29+
### [Example 3]
2830
![](../../images/examples/160_3.png)
2931

3032
**Input**: `listA = [2,6,4], listB = [1,5]`
@@ -40,8 +42,6 @@ Difficulty: **Easy**
4042
**Follow up**: Could you write a solution that runs in `O(m + n)` time and use only `O(1)` memory?
4143

4244
## Intuition
43-
[中文题解](#中文题解)
44-
4545
1. First calculate the number of nodes in the two linked lists A and B. The number of nodes in linked list A is `node_count_a`, and the number of nodes in linked list B is `node_count_b`.
4646
2. If `node_count_b > node_count_a`, then perform `node = node.next` for `node_count_b - node_count_a` times on linked list B.
4747
3. At this time, repeat `node = node.next` on the two linked lists until the same node is found or one of the linked lists has reached the end.
@@ -333,66 +333,3 @@ public class Solution
333333
```
334334
// Welcome to create a PR to complete the code of this language, thanks!
335335
```
336-
337-
## 问题描述
338-
给你两个单链表的头节点 `headA``headB` ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 `null`
339-
340-
图示两个链表在节点 `c1` 开始相交:
341-
342-
![](../../images/examples/160.png)
343-
344-
题目数据 **保证** 整个链式结构中**不存在环**
345-
346-
**注意**,函数返回结果后,链表必须 **保持其原始结构**
347-
348-
难度: **容易**
349-
350-
### [Example 1]
351-
![](../../images/examples/160_1.png)
352-
353-
**输入**: `listA = [4,1,8,4,5], listB = [5,6,1,8,4,5]`
354-
355-
**输出**: `Intersected at '8'`
356-
357-
# 中文题解
358-
## 思路
359-
1. 先把A, B两个链表的节点数计算出来。链表A的节点数为`node_count_a`,链表B的节点数为`node_count_b`
360-
2. 假如`node_count_b > node_count_a`,那么对链表B做`node_count_b - node_count_a``node = node.next` 操作。
361-
3. 这时,两个链表同时重复进行`node = node.next`操作,直到找到相同的节点或者其中一个链表已经到尾部。
362-
363-
## 步骤
364-
1. 先把A, B两个链表的节点数计算出来。链表A的节点数为`node_count_a`,链表B的节点数为`node_count_b`
365-
```python
366-
node_count_a = 0
367-
node_count_b = 0
368-
369-
node = headA
370-
while node:
371-
node_count_a += 1
372-
node = node.next
373-
```
374-
375-
2. 假如`node_count_b > node_count_a`,那么对链表B做`node_count_b - node_count_a``node = node.next` 操作。
376-
```python
377-
bigger = headA
378-
smaller = headB
379-
380-
if node_count_b > node_count_a:
381-
bigger = headB
382-
smaller = headA
383-
384-
for _ in range(abs(node_count_b - node_count_a)):
385-
bigger = bigger.next
386-
```
387-
388-
3. 这时,两个链表同时重复进行`node = node.next`操作,直到找到相同的节点或者其中一个链表已经到尾部。
389-
```python
390-
while bigger and smaller:
391-
if bigger == smaller:
392-
return bigger
393-
394-
bigger = bigger.next
395-
smaller = smaller.next
396-
397-
return None
398-
```

zh/1-1000/160-intersection-of-two-linked-lists.md

Lines changed: 37 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
# 160. Intersection of Two Linked Lists - Best Practices of LeetCode Solutions
2-
LeetCode link: [160. Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists),
3-
[160. 相交链表](https://leetcode.cn/problems/intersection-of-two-linked-lists)
1+
# 160. 相交链表 - 力扣题解最佳实践
2+
力扣链接:[160. 相交链表](https://leetcode.cn/problems/intersection-of-two-linked-lists), 难度: **简单**
43

5-
[中文题解](#中文题解)
6-
7-
## LeetCode problem description
8-
Given the heads of two singly linked-lists `headA` and `headB`, return _the node at which the two lists intersect_. If the two linked lists have no intersection at all, return `null`.
4+
## 力扣“160. 相交链表”问题描述
5+
给你两个单链表的头节点 `headA``headB` ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 `null`
96

10-
For example, the following two linked lists begin to intersect at node `c1`:
7+
图示两个链表在节点 `c1` 开始相交:
118

129
![](../../images/examples/160.png)
1310

14-
The test cases are generated such that there are **no cycles** anywhere in the entire linked structure.
15-
16-
**Note** that the linked lists must **retain their original structure** after the function returns.
11+
题目数据 **保证** 整个链式结构中**不存在环**
1712

18-
Difficulty: **Easy**
13+
**注意**,函数返回结果后,链表必须 **保持其原始结构**
1914

20-
### [Example 1]
15+
### [示例 1]
2116
![](../../images/examples/160_1.png)
2217

23-
**Input**: `listA = [4,1,8,4,5], listB = [5,6,1,8,4,5]`
18+
**输入**: `listA = [4,1,8,4,5], listB = [5,6,1,8,4,5]`
2419

25-
**Output**: `Intersected at '8'`
20+
**输出**: `Intersected at '8'`
2621

27-
### [Example 2]
28-
![](../../images/examples/160_3.png)
22+
### [示例 2]
23+
![](../../images/examples/160_2.png)
24+
25+
**输入**: `intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4]`
2926

30-
**Input**: `listA = [2,6,4], listB = [1,5]`
27+
**输出**: `Intersected at '2'`
3128

32-
**Output**: `No intersection`
29+
### [示例 3]
30+
![](../../images/examples/160_3.png)
31+
32+
**输入**: `listA = [2,6,4], listB = [1,5]`
3333

34-
### [Constraints]
35-
- The number of nodes of `listA` is in the `m`.
36-
- The number of nodes of `listB` is in the `n`.
37-
- `1 <= m, n <= 3 * 10000`
38-
- `1 <= Node.val <= 100000`
34+
**输出**: `No intersection`
3935

40-
**Follow up**: Could you write a solution that runs in `O(m + n)` time and use only `O(1)` memory?
36+
### [约束]
37+
- `listA` 中节点数目为 `m`
38+
- `listB` 中节点数目为 `n`
39+
- `1 <= m, n <= 3 * 10**4`
40+
- `1 <= Node.val <= 10**5`
4141

42-
## Intuition
43-
[中文题解](#中文题解)
42+
**进阶**:你能否设计一个时间复杂度 `O(m + n)` 、仅用 `O(1)` 内存的解决方案?
4443

45-
1. First calculate the number of nodes in the two linked lists A and B. The number of nodes in linked list A is `node_count_a`, and the number of nodes in linked list B is `node_count_b`.
46-
2. If `node_count_b > node_count_a`, then perform `node = node.next` for `node_count_b - node_count_a` times on linked list B.
47-
3. At this time, repeat `node = node.next` on the two linked lists until the same node is found or one of the linked lists has reached the end.
44+
## 思路
45+
1. 先把A, B两个链表的节点数计算出来。链表A的节点数为`node_count_a`,链表B的节点数为`node_count_b`
46+
2. 假如`node_count_b > node_count_a`,那么对链表B做`node_count_b - node_count_a``node = node.next` 操作。
47+
3. 这时,两个链表同时重复进行`node = node.next`操作,直到找到相同的节点或者其中一个链表已经到尾部。
4848

49-
## Steps
50-
1. First calculate the number of nodes in the two linked lists A and B. The number of nodes in linked list A is `node_count_a`, and the number of nodes in linked list B is `node_count_b`.
49+
## 步骤
50+
1. 先把A, B两个链表的节点数计算出来。链表A的节点数为`node_count_a`,链表B的节点数为`node_count_b`
5151
```python
5252
node_count_a = 0
5353
node_count_b = 0
@@ -58,7 +58,7 @@ while node:
5858
node = node.next
5959
```
6060

61-
2. If `node_count_b > node_count_a`, then perform `node = node.next` for `node_count_b - node_count_a` times on linked list B.
61+
2. 假如`node_count_b > node_count_a`,那么对链表B做`node_count_b - node_count_a``node = node.next` 操作。
6262
```python
6363
bigger = headA
6464
smaller = headB
@@ -71,7 +71,7 @@ for _ in range(abs(node_count_b - node_count_a)):
7171
bigger = bigger.next
7272
```
7373

74-
3. At this time, repeat `node = node.next` on the two linked lists until the same node is found or one of the linked lists has reached the end.
74+
3. 这时,两个链表同时重复进行`node = node.next`操作,直到找到相同的节点或者其中一个链表已经到尾部。
7575
```python
7676
while bigger and smaller:
7777
if bigger == smaller:
@@ -83,9 +83,9 @@ while bigger and smaller:
8383
return None
8484
```
8585

86-
## Complexity
87-
* Time: `O(m + n)`.
88-
* Space: `O(1)`.
86+
## 复杂度
87+
* 时间:`O(m + n)`
88+
* 空间:`O(1)`
8989

9090
## Java
9191
```java
@@ -333,66 +333,3 @@ public class Solution
333333
```
334334
// Welcome to create a PR to complete the code of this language, thanks!
335335
```
336-
337-
## 问题描述
338-
给你两个单链表的头节点 `headA``headB` ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 `null`
339-
340-
图示两个链表在节点 `c1` 开始相交:
341-
342-
![](../../images/examples/160.png)
343-
344-
题目数据 **保证** 整个链式结构中**不存在环**
345-
346-
**注意**,函数返回结果后,链表必须 **保持其原始结构**
347-
348-
难度: **容易**
349-
350-
### [Example 1]
351-
![](../../images/examples/160_1.png)
352-
353-
**输入**: `listA = [4,1,8,4,5], listB = [5,6,1,8,4,5]`
354-
355-
**输出**: `Intersected at '8'`
356-
357-
# 中文题解
358-
## 思路
359-
1. 先把A, B两个链表的节点数计算出来。链表A的节点数为`node_count_a`,链表B的节点数为`node_count_b`
360-
2. 假如`node_count_b > node_count_a`,那么对链表B做`node_count_b - node_count_a``node = node.next` 操作。
361-
3. 这时,两个链表同时重复进行`node = node.next`操作,直到找到相同的节点或者其中一个链表已经到尾部。
362-
363-
## 步骤
364-
1. 先把A, B两个链表的节点数计算出来。链表A的节点数为`node_count_a`,链表B的节点数为`node_count_b`
365-
```python
366-
node_count_a = 0
367-
node_count_b = 0
368-
369-
node = headA
370-
while node:
371-
node_count_a += 1
372-
node = node.next
373-
```
374-
375-
2. 假如`node_count_b > node_count_a`,那么对链表B做`node_count_b - node_count_a``node = node.next` 操作。
376-
```python
377-
bigger = headA
378-
smaller = headB
379-
380-
if node_count_b > node_count_a:
381-
bigger = headB
382-
smaller = headA
383-
384-
for _ in range(abs(node_count_b - node_count_a)):
385-
bigger = bigger.next
386-
```
387-
388-
3. 这时,两个链表同时重复进行`node = node.next`操作,直到找到相同的节点或者其中一个链表已经到尾部。
389-
```python
390-
while bigger and smaller:
391-
if bigger == smaller:
392-
return bigger
393-
394-
bigger = bigger.next
395-
smaller = smaller.next
396-
397-
return None
398-
```

0 commit comments

Comments
 (0)