Linked List
Linked List Cycle
DESCRIPTION (inspired by Leetcode.com)
Write a function that takes in a parameter head of type ListNode that is a reference to the head of a linked list. The function should return True if the linked list contains a cycle, and False otherwise, without modifying the linked list in any way.
# Definition of a ListNode class ListNode: def __init__(self, value=0, next=None): self.value = value self.next = next
Example 1:
Output: true, there is a cycle between node 0 and node 3.
Example 2:
Output: false, there is no cycle in the linked list.
Solutions
1. Keep Track of Visited Nodes
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef hasCycle(head):visited_nodes = set()current_node = headwhile current_node is not None:if current_node in visited_nodes:return True # Cycle detectedvisited_nodes.add(current_node)current_node = current_node.nextreturn False
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n) where n is the number of nodes in the linked list. In the worst case scenario, the algorithm visits each node once.
Space Complexity: O(n) where n is the number of nodes in the linked list. The space complexity is due to the set storing visited nodes.
2. Optimal Solution: Fast and Slow Pointers
Detecting a Cycle
initialize pointers
0 / 4
No Cycle
initialize pointers
0 / 3
initialize pointers
0 / 3
- Initialize fast and slow pointers at the head of the linked list.
- Iterate over the linked list. Each iteration advances slow by one node and fast by two nodes.
- If the fast pointer reaches the end of the linked list (either fast.next = None or fast = None), then the linked list does not contain a cycle.
- If the fast and slow pointers meet at the same node (fast == slow), then the linked list contains a cycle.
Code
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef hasCycle(head):slow = headfast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False
linked list cycle
0 / 5
Edge Cases
Empty List
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef hasCycle(head):slow = headfast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False
linked list cycle
0 / 2
Single Node (No Cycle)
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef hasCycle(head):slow = headfast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False
linked list cycle
0 / 2
Single Node (with Cycle)
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef hasCycle(head):slow = headfast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False
linked list cycle
0 / 3
Worst Case Scenario
class ListNode:def __init__(self, val=0, next=None):self.val = valself.next = nextdef hasCycle(head):slow = headfast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False
linked list cycle
0 / 6
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n) where n is the number of nodes in the linked list. In the worst case scenario, the fast pointer traverses the linked list twice, and the slow pointer traverses the linked list once, regardless of the number of nodes in the linked list.
Space Complexity: O(1) We only use two pointers to do our traversal, which does not change with the number of nodes in the linked list.
Mark as read
Unlock Premium Coding Content
On This Page
Your account is free and you can post anonymously if you choose.