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 5c7adb7

Browse files
author
scuyjzh
committed
modify solutions to problem - "142. Linked List Cycle II"
1 parent 713d3f3 commit 5c7adb7

File tree

2 files changed

+28
-56
lines changed

2 files changed

+28
-56
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
| 141 | [环形链表](https://leetcode-cn.com/problems/linked-list-cycle/) | [Java](./src/com/scuyjzh/leetcode/easy/No_0141_Linked_List_Cycle/Solution.java) | 简单 | 哈希表 链表 双指针 |
211211
| 19 | [删除链表的倒数第 N 个节点](https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0019_Remove_Nth_Node_From_End_of_List/Solution.java) | 中等 | 链表 双指针 |
212212
| 876 | [链表的中间结点](https://leetcode-cn.com/problems/middle-of-the-linked-list/) | [Java](./src/com/scuyjzh/leetcode/easy/No_0876_Middle_of_the_Linked_List/Solution.java) | 简单 | 链表 双指针 |
213+
| 142 | [环形链表 II](https://leetcode-cn.com/problems/linked-list-cycle-ii/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0142_Linked_List_Cycle_II/Solution.java) | 中等 | 哈希表 链表 双指针 |
213214

214215
### Array
215216

‎src/com/scuyjzh/leetcode/medium/No_0142_Linked_List_Cycle_II/Solution.java‎

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,34 @@
77
/**
88
* 142. 环形链表 II
99
*
10-
* 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返
11-
* 回 null。
12-
* 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中
13-
* 存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链
14-
* 表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链
15-
* 表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实
16-
* 际情况。
10+
* 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
11+
*
12+
* 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。
13+
* 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中
14+
* 的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。
15+
* 注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
16+
*
1717
* 不允许修改 链表。
18+
*
1819
* 进阶:你是否可以使用 O(1) 空间解决此题?
1920
*/
2021
class Solution {
2122
/**
22-
* 方法一:哈希表
23-
*
24-
* • 时间复杂度:O(N),其中 N 为链表中节点的数目。恰好需要访问链表中的每一个节点。
25-
* • 空间复杂度:O(N),其中 N 为链表中节点的数目。需要将链表中的每个节点都保存在哈希表当
26-
* 中。
23+
* 方法:双指针
2724
*/
28-
public ListNode detectCycle1(ListNode head) {
29-
/*
30-
* 一个非常直观的思路是:遍历链表中的每个节点,并将它记录下来;一旦遇到了此前遍历过的节点,就
31-
* 可以判定链表中存在环。借助哈希表可以很方便地实现。
32-
*/
33-
ListNode pos = head;
34-
Set<ListNode> visited = new HashSet<>();
35-
while (pos != null) {
36-
if (visited.contains(pos)) {
37-
return pos;
38-
} else {
39-
visited.add(pos);
40-
}
41-
pos = pos.next;
42-
}
43-
return null;
44-
}
45-
46-
/**
47-
* 方法二:快慢指针
48-
*
49-
* • 时间复杂度:O(N),其中 N 为链表中节点的数目。在最初判断快慢指针是否相遇时,slow 指针走过
50-
* 的距离不会超过链表的总长度;随后寻找入环点时,走过的距离也不会超过链表的总长度。因此,总的
51-
* 执行时间为 O(N)+O(N)=O(N)。
52-
* • 空间复杂度:O(1)。只使用了 slow,fast,ptr 三个指针。
53-
*/
54-
public ListNode detectCycle2(ListNode head) {
55-
/*
56-
* 使用两个指针,fast 与 slow。它们起始都位于链表的头部。随后,slow 指针每次向后移动一个位置,而
57-
* fast 指针向后移动两个位置。如果链表中存在环,则 fast 指针最终将再次与 slow 指针在环中相遇。
58-
*
59-
* 设链表中环外部分的长度为 a。slow 指针进入环后,又走了 b 的距离与 fast 相遇。此时,fast
60-
* 指针已经走完了环的 n 圈,因此它走过的总距离为 a+n(b+c)+b = a+(n+1)b+nc。
61-
*
62-
* 根据题意,任意时刻,fast 指针走过的距离都为 slow 指针的 2 倍。因此,有
63-
* a+(n+1)b+nc=2(a+b) ⟹ a=c+(n−1)(b+c)
64-
* 有了 a=c+(n−1)(b+c) 的等量关系,会发现:从相遇点到入环点的距离加上 n−1 圈的环长,恰好
65-
* 等于从链表头部到入环点的距离。
66-
*
67-
* 因此,当发现 slow 与 fast 相遇时,再额外使用一个指针 ptr。起始,它指向链表头部;随后,它和 slow
68-
* 每次向后移动一个位置。最终,它们会在入环点相遇。
69-
*/
25+
public ListNode detectCycle(ListNode head) {
26+
// 使用两个指针,fast 与 slow。它们起始都位于链表的头部
7027
ListNode slow = head;
7128
ListNode fast = head;
7229
while (fast != null && fast.next != null) {
30+
// 随后,slow 指针每次向后移动一个位置,而 fast 指针向后移动两个位置
7331
fast = fast.next.next;
7432
slow = slow.next;
75-
// slow 与 fast 相遇
33+
// 如果链表中存在环,则 fast 指针最终将再次与 slow 指针在环中相遇
7634
if (slow == fast) {
35+
// 额外使用一个指针 ptr 指向链表头部
7736
ListNode ptr = head;
37+
// ptr 和 slow 每次向后移动一个位置。最终,它们会在入环点相遇
7838
while (ptr != slow) {
7939
ptr = ptr.next;
8040
slow = slow.next;
@@ -83,7 +43,18 @@ public ListNode detectCycle2(ListNode head) {
8343
return ptr;
8444
}
8545
}
86-
8746
return null;
8847
}
48+
49+
public static void main(String[] args) {
50+
ListNode head = new ListNode(3);
51+
ListNode node1 = new ListNode(2);
52+
ListNode node2 = new ListNode(0);
53+
ListNode node3 = new ListNode(-4);
54+
head.next = node1;
55+
node1.next = node2;
56+
node2.next = node3;
57+
node3.next = node1;
58+
System.out.println(new Solution().detectCycle(head).val);
59+
}
8960
}

0 commit comments

Comments
(0)

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