Easy
Given head
, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next
pointer. Internally, pos
is used to denote the index of the node that tail’s next
pointer is connected to. Note that pos
is not passed as a parameter.
Return true
if there is a cycle in the linked list. Otherwise, return false
.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Constraints:
[0, 104]
.-105 <= Node.val <= 105
pos
is -1
or a valid index in the linked-list.Follow up: Can you solve it using O(1)
(i.e. constant) memory?
To solve the “Linked List Cycle” problem, we can use the Floyd’s Tortoise and Hare algorithm. This algorithm uses two pointers moving at different speeds to detect if a cycle exists in the linked list. Here’s a step-by-step guide and the Swift implementation of the solution.
slow
and fast
, both starting at the head of the linked list.slow
pointer one step at a time.fast
pointer two steps at a time.fast
pointer will eventually meet the slow
pointer.fast
pointer reaches the end of the list (i.e., fast
or fast.next
becomes nil
), there is no cycle in the linked list.fast
pointer meets the slow
pointer, return true
indicating a cycle.fast
pointer reaches the end, return false
.Here’s the implementation of the Solution
class using the Floyd’s Tortoise and Hare algorithm:
// Definition for singly-linked list.
class ListNode {
var val: Int
var next: ListNode?
init(_ val: Int) {
self.val = val
self.next = nil
}
}
class Solution {
func hasCycle(_ head: ListNode?) -> Bool {
// Initialize slow and fast pointers
var slow = head
var fast = head
// Traverse the linked list
while fast != nil && fast?.next != nil {
slow = slow?.next // Move slow pointer one step
fast = fast?.next?.next // Move fast pointer two steps
// Check if slow and fast pointers meet
if slow === fast {
return true
}
}
// If we reach here, there is no cycle
return false
}
}
ListNode
class defines the structure of a node in the linked list.Solution
class contains the hasCycle
method which checks for a cycle.while
loop continues as long as fast
and fast?.next
are not nil
.slow
pointer is moved one step (slow = slow?.next
).fast
pointer is moved two steps (fast = fast?.next?.next
).slow
pointer and fast
pointer meet (if slow === fast
), it indicates there is a cycle, and the method returns true
.fast
or fast?.next
is nil
, it means there is no cycle, and the method returns false
.This approach ensures that we use constant space (O(1)
) while efficiently detecting the presence of a cycle in the linked list.