How to Detect Cycle in Linked List JavaScript: Simple Guide
To detect a cycle in a linked list in JavaScript, use the
Floyd’s Cycle Detection algorithm, also called the tortoise and hare method. It uses two pointers moving at different speeds; if they meet, a cycle exists.Syntax
The main idea is to use two pointers: slow moves one step at a time, and fast moves two steps at a time. If the linked list has a cycle, these pointers will eventually point to the same node.
Function signature:
function hasCycle(head) { ... }Where head is the first node of the linked list.
javascript
function hasCycle(head) { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; // cycle detected } } return false; // no cycle }
Example
This example creates a linked list with a cycle and uses the hasCycle function to detect it. It prints true if a cycle exists, otherwise false.
javascript
class ListNode { constructor(value) { this.value = value; this.next = null; } } // Create nodes const node1 = new ListNode(1); const node2 = new ListNode(2); const node3 = new ListNode(3); const node4 = new ListNode(4); // Link nodes node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node2; // Creates a cycle back to node2 function hasCycle(head) { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; if (slow === fast) { return true; } } return false; } console.log(hasCycle(node1));
Output
true
Common Pitfalls
- Not checking if
fastorfast.nextisnullbefore moving pointers can cause errors. - Comparing node values instead of node references will not detect cycles correctly.
- Forgetting to move both pointers properly can cause infinite loops.
javascript
/* Wrong approach: comparing values instead of nodes */ function wrongHasCycle(head) { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; if (slow.value === fast.value) { // Wrong: values can be same but no cycle return true; } } return false; } /* Correct approach: compare nodes directly */ function correctHasCycle(head) { let slow = head; let fast = head; while (fast !== null && fast.next !== null) { slow = slow.next; fast = fast.next.next; if (slow === fast) { // Correct: nodes are same return true; } } return false; }
Quick Reference
Cycle Detection Tips:
- Use two pointers: slow (1 step), fast (2 steps).
- Check pointers for
nullbefore advancing. - Compare nodes by reference, not by value.
- If pointers meet, cycle exists.
- If
fastreachesnull, no cycle.
Key Takeaways
Use Floyd’s Cycle Detection algorithm with two pointers moving at different speeds.
Always check for null pointers before advancing to avoid runtime errors.
Compare nodes by reference, not by their values, to detect cycles correctly.
If the fast and slow pointers meet, a cycle exists in the linked list.
If the fast pointer reaches the end (null), the list has no cycle.