题目描述
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
我的解法
解题思路
一开始完全没有思路,看了下题解才发现其实可以用set来判断有没有出现重复的node
1 | /** |
Runtime: 20 ms, faster than 17.13% of C++ online submissions for Linked List Cycle.
Memory Usage: 12.2 MB, less than 10.53% of C++ online submissions for Linked List Cycle.
快慢指针
用两个指针,慢指针一次移动一步,快指针一次移动两步,然后判断快指针和慢指针会不会相等,如果相等则存在环。
1 | /** |
Runtime: 8 ms, faster than 97.20% of C++ online submissions for Linked List Cycle.
Memory Usage: 9.9 MB, less than 43.42% of C++ online submissions for Linked List Cycle.