Skip to content

Commit 9be79e6

Browse files
committed
+ problem 142
1 parent 7369de7 commit 9be79e6

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 142. Linked List Cycle II
2+
Given the `head` of a linked list, return *the node where the cycle begins. If there is no cycle, return* `null`.
3+
4+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to (**0-indexed**). It is `-1` if there is no cycle. **Note that** `pos` **is not passed as a parameter**.
5+
6+
**Do not modify** the linked list.
7+
8+
#### Example 1:
9+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)
10+
<pre>
11+
<strong>Input:</strong> head = [3,2,0,-4], pos = 1
12+
<strong>Output:</strong> tail connects to node index 1
13+
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node.
14+
</pre>
15+
16+
#### Example 2:
17+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)
18+
<pre>
19+
<strong>Input:</strong> head = [1,2], pos = 0
20+
<strong>Output:</strong> tail connects to node index 0
21+
<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node.
22+
</pre>
23+
24+
#### Example 3:
25+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)
26+
<pre>
27+
<strong>Input:</strong> head = [1], pos = -1
28+
<strong>Output:</strong> no cycle
29+
<strong>Explanation:</strong> There is no cycle in the linked list.
30+
</pre>
31+
32+
#### Constraints:
33+
* The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
34+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
35+
* `pos` is `-1` or a **valid index** in the linked-list.
36+
37+
**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory?
38+
39+
## Solutions (Python)
40+
41+
### 1. Solution
42+
```Python
43+
# Definition for singly-linked list.
44+
# class ListNode:
45+
# def __init__(self, x):
46+
# self.val = x
47+
# self.next = None
48+
49+
class Solution:
50+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
51+
if head is None or head.next is None:
52+
return None
53+
54+
slow = head.next
55+
fast = head.next.next
56+
57+
while slow != fast:
58+
if fast is None or fast.next is None:
59+
return None
60+
61+
slow = slow.next
62+
fast = fast.next.next
63+
64+
while head != slow:
65+
head = head.next
66+
slow = slow.next
67+
68+
return head
69+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 142. 环形链表 II
2+
给定一个链表的头节点 `head` ,返回链表开始入环的第一个节点。 *如果链表无环,则返回 `null`*
3+
4+
如果链表中有某个节点,可以通过连续跟踪 `next` 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 `pos` 来表示链表尾连接到链表中的位置(**索引从 0 开始**)。如果 `pos``-1`,则在该链表中没有环。**注意:**`pos` **不作为参数进行传递**,仅仅是为了标识链表的实际情况。
5+
6+
**不允许修改** 链表。
7+
8+
#### 示例 1:
9+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)
10+
<pre>
11+
<strong>输入:</strong> head = [3,2,0,-4], pos = 1
12+
<strong>输出:</strong> 返回索引为 1 的链表节点
13+
<strong>解释:</strong> 链表中有一个环,其尾部连接到第二个节点。
14+
</pre>
15+
16+
#### 示例 2:
17+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)
18+
<pre>
19+
<strong>输入:</strong> head = [1,2], pos = 0
20+
<strong>输出:</strong> 返回索引为 0 的链表节点
21+
<strong>解释:</strong> 链表中有一个环,其尾部连接到第一个节点。
22+
</pre>
23+
24+
#### 示例 3:
25+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)
26+
<pre>
27+
<strong>输入:</strong> head = [1], pos = -1
28+
<strong>输出:</strong> 返回 null
29+
<strong>解释:</strong> 链表中没有环。
30+
</pre>
31+
32+
#### 提示:
33+
* 链表中节点的数目范围在范围 <code>[0, 10<sup>4</sup>]</code> 内
34+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
35+
* `pos` 的值为 `-1` 或者链表中的一个有效索引
36+
37+
**进阶:**你是否可以使用 `O(1)` 空间解决此题?
38+
39+
## 题解 (Python)
40+
41+
### 1. 题解
42+
```Python
43+
# Definition for singly-linked list.
44+
# class ListNode:
45+
# def __init__(self, x):
46+
# self.val = x
47+
# self.next = None
48+
49+
class Solution:
50+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
51+
if head is None or head.next is None:
52+
return None
53+
54+
slow = head.next
55+
fast = head.next.next
56+
57+
while slow != fast:
58+
if fast is None or fast.next is None:
59+
return None
60+
61+
slow = slow.next
62+
fast = fast.next.next
63+
64+
while head != slow:
65+
head = head.next
66+
slow = slow.next
67+
68+
return head
69+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
9+
if head is None or head.next is None:
10+
return None
11+
12+
slow = head.next
13+
fast = head.next.next
14+
15+
while slow != fast:
16+
if fast is None or fast.next is None:
17+
return None
18+
19+
slow = slow.next
20+
fast = fast.next.next
21+
22+
while head != slow:
23+
head = head.next
24+
slow = slow.next
25+
26+
return head

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
[138][138l] |[Copy List with Random Pointer][138] |![py]
106106
[139][139l] |[Word Break][139] |![py]&nbsp;&nbsp;![rb]
107107
[141][141l] |[Linked List Cycle][141] |![py]
108+
[142][142l] |[Linked List Cycle II][142] |![py]
108109
[144][144l] |[Binary Tree Preorder Traversal][144] |![py]
109110
[145][145l] |[Binary Tree Postorder Traversal][145] |![py]
110111
[147][147l] |[Insertion Sort List][147] |![py]
@@ -1331,6 +1332,7 @@
13311332
[138]:Problemset/0138-Copy%20List%20with%20Random%20Pointer/README.md#138-copy-list-with-random-pointer
13321333
[139]:Problemset/0139-Word%20Break/README.md#139-word-break
13331334
[141]:Problemset/0141-Linked%20List%20Cycle/README.md#141-linked-list-cycle
1335+
[142]:Problemset/0142-Linked%20List%20Cycle%20II/README.md#142-linked-list-cycle-ii
13341336
[144]:Problemset/0144-Binary%20Tree%20Preorder%20Traversal/README.md#144-binary-tree-preorder-traversal
13351337
[145]:Problemset/0145-Binary%20Tree%20Postorder%20Traversal/README.md#145-binary-tree-postorder-traversal
13361338
[147]:Problemset/0147-Insertion%20Sort%20List/README.md#147-insertion-sort-list

README_CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
[138][138l] |[复制带随机指针的链表][138] |![py]
106106
[139][139l] |[单词拆分][139] |![py]&nbsp;&nbsp;![rb]
107107
[141][141l] |[环形链表][141] |![py]
108+
[142][142l] |[环形链表 II][142] |![py]
108109
[144][144l] |[二叉树的前序遍历][144] |![py]
109110
[145][145l] |[二叉树的后序遍历][145] |![py]
110111
[147][147l] |[对链表进行插入排序][147] |![py]
@@ -1331,6 +1332,7 @@
13311332
[138]:Problemset/0138-Copy%20List%20with%20Random%20Pointer/README_CN.md#138-复制带随机指针的链表
13321333
[139]:Problemset/0139-Word%20Break/README_CN.md#139-单词拆分
13331334
[141]:Problemset/0141-Linked%20List%20Cycle/README_CN.md#141-环形链表
1335+
[142]:Problemset/0142-Linked%20List%20Cycle%20II/README_CN.md#142-环形链表-ii
13341336
[144]:Problemset/0144-Binary%20Tree%20Preorder%20Traversal/README_CN.md#144-二叉树的前序遍历
13351337
[145]:Problemset/0145-Binary%20Tree%20Postorder%20Traversal/README_CN.md#145-二叉树的后序遍历
13361338
[147]:Problemset/0147-Insertion%20Sort%20List/README_CN.md#147-对链表进行插入排序

0 commit comments

Comments
 (0)