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 29ed3f5

Browse files
author
scuyjzh
committed
modify solutions to problem - "141. Linked List Cycle"
1 parent d1377a4 commit 29ed3f5

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@
203203
| 220 | [存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [Java](./src/com/scuyjzh/leetcode/medium/No_0220_Contains_Duplicate_III/Solution.java) | 中等 | 数组 桶排序 有序集合 排序 滑动窗口 |
204204
| 1438 | [绝对差不超过限制的最长连续子数组](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [Java](./src/com/scuyjzh/leetcode/medium/No_1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit/Solution.java) | 中等 | 队列 数组 有序集合 滑动窗口 单调队列 堆(优先队列) |
205205

206+
#### 链表中的双指针问题
207+
208+
| # | 题目 | 题解 | 难度 | 标签 |
209+
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ------------------ |
210+
| 141 | [环形链表](https://leetcode-cn.com/problems/linked-list-cycle/) | [Java](./src/com/scuyjzh/leetcode/easy/No_0141_Linked_List_Cycle/Solution.java) | 简单 | 哈希表 链表 双指针 |
211+
206212
### Array
207213

208214
| # | 题目 | 题解 | 难度 | 标签 |

‎src/com/scuyjzh/leetcode/easy/No_0141_Linked_List_Cycle/Solution.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
* 141. 环形链表
77
*
88
* 给你一个链表的头节点 head ,判断链表中是否有环。
9+
*
910
* 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表
1011
* 中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示
11-
* 链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该
12-
* 链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的
13-
* 实际情况。
14-
* 如果链表中存在环,则返回 true 。 否则,返回 false 。
12+
* 链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进
13+
* 行传递 。仅仅是为了标识链表的实际情况。
14+
*
15+
* 如果链表中存在环,则返回 true 。 否则,返回 false 。
1516
*/
1617
class Solution {
1718
/**
18-
* 方法:快慢指针
19+
* 方法:双指针
1920
*/
2021
public boolean hasCycle(ListNode head) {
2122
/*
@@ -35,8 +36,10 @@ public boolean hasCycle(ListNode head) {
3536
return false;
3637
}
3738

38-
// 定义两个指针,一快一满
39-
// 初始时,慢指针在位置 head,而快指针在位置 head.next
39+
// 定义两个指针,一快一满。初始时,慢指针在位置 head,而快指针在位置 head.next。
40+
// 由于循环条件一定是判断快慢指针是否重合,如果将两个指针初始都置于 head,那么 while 循环就不会执行。
41+
// 因此,可以假想一个在 head 之前的虚拟节点,慢指针从虚拟节点移动一步到达 head,快指针从虚拟节点移动两步到达 head.next,
42+
// 这样就可以使用 while 循环了。
4043
ListNode slow = head;
4144
ListNode fast = head.next;
4245
// 如果在移动的过程中,快指针反过来追上慢指针,就说明该链表为环形链表

0 commit comments

Comments
(0)

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