-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 141. 环形链表 | ||
|
||
给定一个链表,判断链表中是否有环。 | ||
|
||
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。 | ||
|
||
如果链表中存在环,则返回 true 。 否则,返回 false 。 | ||
|
||
来源:力扣(LeetCode) | ||
|
||
链接:<https://leetcode-cn.com/problems/linked-list-cycle> | ||
|
||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
|
||
--- | ||
|
||
哈希表也可以,记录某一个到没到过。 | ||
|
||
```js | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val) { | ||
* this.val = val; | ||
* this.next = null; | ||
* } | ||
*/ | ||
|
||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var hasCycle = function(head) { | ||
if (!head) return false | ||
var slow = head, fast = head.next | ||
while (true) { | ||
if (!fast || !fast.next || !fast.next.next || !slow || !slow.next) return false | ||
if (fast === slow) return true | ||
fast = fast.next.next | ||
slow = slow.next | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
# Leet-Code-Solus | ||
|
||
Leet Code Solutions Repo | ||
|
||
--- | ||
|
||
## 数组类问题 | ||
|
||
- 双指针,一个在头,一个在尾 | ||
- 双指针,一个比一个快一步 | ||
- 动态规划 | ||
|
||
## 链表类问题 | ||
|
||
- 递归 | ||
- 单向遍历 | ||
- 快慢指针 | ||
- 一个比一个早走 n 步 | ||
- 一个一次走一步,一个一次走两步 |