Skip to content

Commit eeeeab2

Browse files
committed
141. 环形链表
1 parent 874d5fa commit eeeeab2

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

0141. 环形链表.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 141. 环形链表
2+
3+
给定一个链表,判断链表中是否有环。
4+
5+
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
6+
7+
如果链表中存在环,则返回 true 。 否则,返回 false 。
8+
9+
来源:力扣(LeetCode)
10+
11+
链接:<https://leetcode-cn.com/problems/linked-list-cycle>
12+
13+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
14+
15+
---
16+
17+
哈希表也可以,记录某一个到没到过。
18+
19+
```js
20+
/**
21+
* Definition for singly-linked list.
22+
* function ListNode(val) {
23+
* this.val = val;
24+
* this.next = null;
25+
* }
26+
*/
27+
28+
/**
29+
* @param {ListNode} head
30+
* @return {boolean}
31+
*/
32+
var hasCycle = function(head) {
33+
if (!head) return false
34+
var slow = head, fast = head.next
35+
while (true) {
36+
if (!fast || !fast.next || !fast.next.next || !slow || !slow.next) return false
37+
if (fast === slow) return true
38+
fast = fast.next.next
39+
slow = slow.next
40+
}
41+
};
42+
```

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
# Leet-Code-Solus
22

33
Leet Code Solutions Repo
4+
5+
---
6+
7+
## 数组类问题
8+
9+
- 双指针,一个在头,一个在尾
10+
- 双指针,一个比一个快一步
11+
- 动态规划
12+
13+
## 链表类问题
14+
15+
- 递归
16+
- 单向遍历
17+
- 快慢指针
18+
- 一个比一个早走 n 步
19+
- 一个一次走一步,一个一次走两步

0 commit comments

Comments
 (0)