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

Commit b99c647

Browse files
添加第61题
1 parent 7b6a30e commit b99c647

File tree

7 files changed

+173
-1
lines changed

7 files changed

+173
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode id=142 lang=cpp
3+
*
4+
* [142] Linked List Cycle II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode(int x) : val(x), next(NULL) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
ListNode *detectCycle(ListNode *head) {
19+
if (head == nullptr)
20+
return nullptr;
21+
ListNode *fast, *slow;
22+
fast = slow = head;
23+
while (fast->next != nullptr && fast->next->next != nullptr)
24+
{
25+
fast = fast->next->next;
26+
slow = slow->next;
27+
// 有环
28+
if (fast == slow)
29+
{
30+
// slow 从head从新走,相遇即为环起点
31+
slow = head;
32+
while (fast != slow)
33+
{
34+
fast = fast->next;
35+
slow = slow->next;
36+
}
37+
return fast;
38+
}
39+
}
40+
return nullptr;
41+
}
42+
};
43+
// @lc code=end
44+
53.2 KB
Loading[フレーム]
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# 环形链表2
2+
3+
#### *Linked List Cycle II*
4+
5+
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
6+
7+
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
8+
9+
说明:不允许修改给定的链表。
10+
11+
进阶:
12+
13+
你是否可以使用 O(1) 空间解决此题?
14+
15+
16+
英文题目:
17+
18+
Given the `head` of a linked list, return the node where the cycle begins. If there is no cycle, return null.
19+
20+
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.
21+
22+
Do not modify the linked list.
23+
24+
25+
26+
**example 1**
27+
![test 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)/circularlinkedlist.png)
28+
29+
```
30+
Input: head = [3,2,0,-4], pos = 1
31+
Output: true
32+
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
33+
34+
```
35+
36+
37+
**example 2**
38+
![test 2](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)/circularlinkedlist_test2.png)
39+
40+
```
41+
Input: head = [1,2], pos = 0
42+
Output: tail connects to node index 0
43+
Explanation: There is a cycle in the linked list, where tail connects to the first node.
44+
45+
```
46+
47+
**example 3**
48+
![test 3](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)/circularlinkedlist_test3.png)
49+
50+
```
51+
Input: head = [1], pos = -1
52+
Output: no cycle
53+
Explanation: There is no cycle in the linked list.
54+
55+
```
56+
57+
58+
**Constraints:**
59+
60+
The number of the nodes in the list is in the range [0, 104].
61+
62+
-105 <= Node.val <= 105
63+
64+
pos is -1 or a **valid index** in the linked-list.
65+
66+
67+
---
68+
69+
### 思路
70+
71+
1. 参照[Linked List Cycle](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/60.%20Linked%20List%20Cycle(Easy))可判断链表是否有环
72+
2. 若链表有环,在相遇处让其中一个指针指向头节点`head`后,两个指针同时前进,再次相遇的节点即为环的起始节点
73+
3. 具体原理如下图
74+
![img](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)/2.jpeg)
75+
76+
77+
### 代码
78+
```
79+
80+
/*
81+
* @lc app=leetcode id=142 lang=cpp
82+
*
83+
* [142] Linked List Cycle II
84+
*/
85+
86+
// @lc code=start
87+
/**
88+
* Definition for singly-linked list.
89+
* struct ListNode {
90+
* int val;
91+
* ListNode *next;
92+
* ListNode(int x) : val(x), next(NULL) {}
93+
* };
94+
*/
95+
class Solution {
96+
public:
97+
ListNode *detectCycle(ListNode *head) {
98+
if (head == nullptr)
99+
return nullptr;
100+
ListNode *fast, *slow;
101+
fast = slow = head;
102+
while (fast->next != nullptr && fast->next->next != nullptr)
103+
{
104+
fast = fast->next->next;
105+
slow = slow->next;
106+
// 有环
107+
if (fast == slow)
108+
{
109+
// slow 从head从新走,相遇即为环起点
110+
slow = head;
111+
while (fast != slow)
112+
{
113+
fast = fast->next;
114+
slow = slow->next;
115+
}
116+
return fast;
117+
}
118+
}
119+
return nullptr;
120+
}
121+
};
122+
// @lc code=end
123+
124+
125+
```
126+
127+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
11 KB
Loading[フレーム]
4.47 KB
Loading[フレーム]
1.92 KB
Loading[フレーム]

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@
5757
##### 57. [78.Subsets](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/57.%20Subsets(Medium)) 子集
5858
##### 58. [79.Word Search](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/58.%20Word%20Search(Medium)) 单词搜索
5959
##### 59. [80.Remove Duplicates from Sorted Array II](https://github.com/SherlockUnknowEn/leetcode/tree/master/50-59/59.%20Remove%20Duplicates%20from%20Sorted%20Array%20II(Medium)) 删除排序数组中的重复项 II
60-
##### 60. [141.Linked List Cycle](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/60.%20Linked%20List%20Cycle(Easy)) 环形链表
60+
##### 60. [141.Linked List Cycle](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/60.%20Linked%20List%20Cycle(Easy)) 环形链表
61+
##### 61. [142.Linked List Cycle II](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/61.%20Linked%20List%20Cycle%20II(Medium)) 环形链表2

0 commit comments

Comments
(0)

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