Skip to content

Commit ce1f378

Browse files
环形链表提交
1 parent 2def910 commit ce1f378

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* @lc app=leetcode.cn id=142 lang=cpp
3+
*
4+
* [142] 环形链表 II
5+
*
6+
* https://leetcode-cn.com/problems/linked-list-cycle-ii/description/
7+
*
8+
* algorithms
9+
* Medium (49.55%)
10+
* Likes: 421
11+
* Dislikes: 0
12+
* Total Accepted: 64.2K
13+
* Total Submissions: 129.6K
14+
* Testcase Example: '[3,2,0,-4]\n1'
15+
*
16+
* 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
17+
*
18+
* 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
19+
*
20+
* 说明:不允许修改给定的链表。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
* 输入:head = [3,2,0,-4], pos = 1
27+
* 输出:tail connects to node index 1
28+
* 解释:链表中有一个环,其尾部连接到第二个节点。
29+
*
30+
*
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
* 输入:head = [1,2], pos = 0
36+
* 输出:tail connects to node index 0
37+
* 解释:链表中有一个环,其尾部连接到第一个节点。
38+
*
39+
*
40+
*
41+
*
42+
* 示例 3:
43+
*
44+
* 输入:head = [1], pos = -1
45+
* 输出:no cycle
46+
* 解释:链表中没有环。
47+
*
48+
*
49+
*
50+
*
51+
*
52+
*
53+
* 进阶:
54+
* 你是否可以不用额外空间解决此题?
55+
*
56+
*/
57+
58+
// @lc code=start
59+
/**
60+
* Definition for singly-linked list.
61+
* struct ListNode {
62+
* int val;
63+
* ListNode *next;
64+
* ListNode(int x) : val(x), next(NULL) {}
65+
* };
66+
*/
67+
class Solution {
68+
public:
69+
ListNode *detectCycle(ListNode *head) {
70+
ListNode* slowNode = head;
71+
ListNode* fastNode = head;
72+
73+
while(true){
74+
if (fastNode == NULL || fastNode->next == NULL) {
75+
return NULL;
76+
}
77+
fastNode = fastNode->next->next;
78+
slowNode = slowNode->next;
79+
if(slowNode == fastNode){
80+
break;
81+
}
82+
}
83+
fastNode = head;
84+
while(fastNode != slowNode){
85+
fastNode = fastNode->next;
86+
slowNode = slowNode->next;
87+
}
88+
return fastNode;
89+
}
90+
};
91+
// @lc code=end
92+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* @lc app=leetcode.cn id=142 lang=java
3+
*
4+
* [142] 环形链表 II
5+
*
6+
* https://leetcode-cn.com/problems/linked-list-cycle-ii/description/
7+
*
8+
* algorithms
9+
* Medium (49.55%)
10+
* Likes: 421
11+
* Dislikes: 0
12+
* Total Accepted: 64.2K
13+
* Total Submissions: 129.6K
14+
* Testcase Example: '[3,2,0,-4]\n1'
15+
*
16+
* 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
17+
*
18+
* 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
19+
*
20+
* 说明:不允许修改给定的链表。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
* 输入:head = [3,2,0,-4], pos = 1
27+
* 输出:tail connects to node index 1
28+
* 解释:链表中有一个环,其尾部连接到第二个节点。
29+
*
30+
*
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
* 输入:head = [1,2], pos = 0
36+
* 输出:tail connects to node index 0
37+
* 解释:链表中有一个环,其尾部连接到第一个节点。
38+
*
39+
*
40+
*
41+
*
42+
* 示例 3:
43+
*
44+
* 输入:head = [1], pos = -1
45+
* 输出:no cycle
46+
* 解释:链表中没有环。
47+
*
48+
*
49+
*
50+
*
51+
*
52+
*
53+
* 进阶:
54+
* 你是否可以不用额外空间解决此题?
55+
*
56+
*/
57+
58+
// @lc code=start
59+
/**
60+
* Definition for singly-linked list.
61+
* class ListNode {
62+
* int val;
63+
* ListNode next;
64+
* ListNode(int x) {
65+
* val = x;
66+
* next = null;
67+
* }
68+
* }
69+
*/
70+
public class Solution {
71+
public ListNode detectCycle(ListNode head) {
72+
ListNode fastNode = head, slowNode = head;
73+
74+
while(true) {
75+
if(fastNode == null || fastNode.next == null){
76+
return null;
77+
}
78+
fastNode = fastNode.next.next;
79+
slowNode = slowNode.next;
80+
if(fastNode == slowNode){
81+
break;
82+
}
83+
}
84+
fastNode = head;
85+
while (fastNode != slowNode) {
86+
fastNode = fastNode.next;
87+
slowNode = slowNode.next;
88+
}
89+
return fastNode;
90+
}
91+
}
92+
// @lc code=end
93+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#
2+
# @lc app=leetcode.cn id=142 lang=python3
3+
#
4+
# [142] 环形链表 II
5+
#
6+
# https://leetcode-cn.com/problems/linked-list-cycle-ii/description/
7+
#
8+
# algorithms
9+
# Medium (49.55%)
10+
# Likes: 421
11+
# Dislikes: 0
12+
# Total Accepted: 64.2K
13+
# Total Submissions: 129.6K
14+
# Testcase Example: '[3,2,0,-4]\n1'
15+
#
16+
# 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
17+
#
18+
# 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
19+
#
20+
# 说明:不允许修改给定的链表。
21+
#
22+
#
23+
#
24+
# 示例 1:
25+
#
26+
# 输入:head = [3,2,0,-4], pos = 1
27+
# 输出:tail connects to node index 1
28+
# 解释:链表中有一个环,其尾部连接到第二个节点。
29+
#
30+
#
31+
#
32+
#
33+
# 示例 2:
34+
#
35+
# 输入:head = [1,2], pos = 0
36+
# 输出:tail connects to node index 0
37+
# 解释:链表中有一个环,其尾部连接到第一个节点。
38+
#
39+
#
40+
#
41+
#
42+
# 示例 3:
43+
#
44+
# 输入:head = [1], pos = -1
45+
# 输出:no cycle
46+
# 解释:链表中没有环。
47+
#
48+
#
49+
#
50+
#
51+
#
52+
#
53+
# 进阶:
54+
# 你是否可以不用额外空间解决此题?
55+
#
56+
#
57+
58+
# @lc code=start
59+
# Definition for singly-linked list.
60+
# class ListNode:
61+
# def __init__(self, x):
62+
# self.val = x
63+
# self.next = None
64+
65+
class Solution:
66+
def detectCycle(self, head: ListNode) -> ListNode:
67+
slowNode = head
68+
fastNode = head
69+
70+
while True:
71+
if (fastNode is None or fastNode.next is None):
72+
return None
73+
fastNode = fastNode.next.next
74+
slowNode = slowNode.next
75+
if fastNode == slowNode:
76+
break
77+
fastNode = head
78+
while fastNode != slowNode :
79+
fastNode = fastNode.next
80+
slowNode = slowNode.next
81+
return fastNode
82+
83+
84+
# @lc code=end
85+

0 commit comments

Comments
 (0)