What does it mean for a linked list to be circular?
A linked list is circular if its last node points back to any previous node in the list, creating a loop instead of ending with null.
Click to reveal answer
beginner
Which two pointers are commonly used to detect a cycle in a linked list?
The 'slow' pointer moves one step at a time, and the 'fast' pointer moves two steps at a time. If they meet, the list is circular.
Click to reveal answer
intermediate
Why does the fast pointer moving two steps help detect a cycle?
Because if there is a cycle, the fast pointer will eventually catch up to the slow pointer inside the loop, proving the list is circular.
Click to reveal answer
intermediate
What is the time complexity of detecting a cycle using the two-pointer method?
The time complexity is O(n), where n is the number of nodes, because each pointer moves through the list at most once.
Click to reveal answer
beginner
Show the Go code snippet to detect if a linked list is circular using two pointers.
func isCircular(head *Node) bool {
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
if slow == fast {
return true
}
}
return false
}
Click to reveal answer
What does the fast pointer do in cycle detection?
AMoves one step at a time
BMoves two steps at a time
CMoves three steps at a time
DDoes not move
✗ Incorrect
The fast pointer moves two steps at a time to catch the slow pointer if a cycle exists.
If a linked list has no cycle, what will the fast pointer eventually reach?
AA null (end of list)
BThe head node
CThe slow pointer
DIt will loop forever
✗ Incorrect
Without a cycle, the fast pointer will reach the end of the list, which is null.
What is the main idea behind using two pointers to detect a cycle?
AOne moves faster and can catch the other in a loop
BThey move at the same speed
CThey move in opposite directions
DThey jump randomly
✗ Incorrect
The fast pointer moves faster and will catch the slow pointer if a cycle exists.
What happens if slow and fast pointers meet in the list?
AThe list has no cycle
BThe list is empty
CThe list is circular
DThe pointers reset
✗ Incorrect
Meeting means the list has a cycle, so it is circular.
What is the space complexity of the two-pointer cycle detection method?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
The method uses only two pointers, so it uses constant extra space O(1).
Explain how to detect if a linked list is circular using two pointers.
Think about two runners on a track, one faster than the other.
You got /4 concepts.
Describe why the two-pointer method is efficient for cycle detection in linked lists.
Consider both time and space efficiency.
You got /4 concepts.
Practice
(1/5)
1. What does it mean if a linked list is circular?
easy
A. The last node points back to one of the previous nodes, forming a loop
B. The linked list has no nodes
C. The linked list nodes are sorted in ascending order
D. The linked list has only one node
Solution
Step 1: Understand linked list structure
A linked list is a chain of nodes where each node points to the next node.
Step 2: Define circular linked list
If the last node points back to any previous node, it creates a loop, making the list circular.
Final Answer:
The last node points back to one of the previous nodes, forming a loop -> Option A
Quick Check:
Circular linked list means loop in nodes = D [OK]
Hint: Circular means last node links back inside list [OK]
Common Mistakes:
Thinking circular means sorted list
Confusing empty list with circular
Assuming single node list is always circular
2. Which Go code snippet correctly checks if a linked list is circular using two pointers?
easy
A. for slow != nil && fast != nil { slow = slow.Next; fast = fast.Next; if slow == fast { return true } } return false
B. for slow != nil { slow = slow.Next; if slow == fast { return true } } return false
C. for slow != nil && fast != nil && fast.Next != nil { slow = slow.Next; fast = fast.Next.Next; if slow == fast { return true } } return false
D. for fast != nil && fast.Next != nil { fast = fast.Next.Next; if slow == fast { return true } } return false
Solution
Step 1: Understand Floyd's cycle detection
Use two pointers: slow moves one step, fast moves two steps. If they meet, list is circular.
Step 2: Check code correctness
for slow != nil && fast != nil && fast.Next != nil { slow = slow.Next; fast = fast.Next.Next; if slow == fast { return true } } return false correctly moves slow by one and fast by two steps and checks for equality inside the loop.
Final Answer:
for slow != nil && fast != nil && fast.Next != nil { slow = slow.Next; fast = fast.Next.Next; if slow == fast { return true } } return false -> Option C
Quick Check:
Two pointers moving at different speeds detect loop = B [OK]
Hint: Fast pointer moves twice, slow moves once to detect loop [OK]
Common Mistakes:
Moving fast pointer only one step
Not checking fast.Next for nil
Checking slow == fast outside loop
3. What will be the output of this Go code snippet detecting circular linked list?
type Node struct { Val int; Next *Node }
func isCircular(head *Node) bool {
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
if slow == fast {
return true
}
}
return false
}
// List: 1 -> 2 -> 3 -> 4 -> 2 (loop back to node with value 2)
node1 := &Node{1, nil}
node2 := &Node{2, nil}
node3 := &Node{3, nil}
node4 := &Node{4, nil}
node1.Next = node2
node2.Next = node3
node3.Next = node4
node4.Next = node2
fmt.Println(isCircular(node1))
medium
A. true
B. false
C. runtime error
D. infinite loop
Solution
Step 1: Analyze linked list structure
The last node (node4) points back to node2, creating a loop.
Step 2: Trace Floyd's cycle detection
Slow and fast pointers will eventually meet inside the loop, so function returns true.
Final Answer:
true -> Option A
Quick Check:
Loop exists, so output is true [OK]
Hint: Loop in list means function returns true [OK]
Common Mistakes:
Assuming no loop returns true
Confusing pointer equality with value equality
Ignoring fast.Next nil check
4. Find the bug in this Go code that tries to detect if a linked list is circular:
func isCircular(head *Node) bool {
slow, fast := head, head
for fast != nil {
slow = slow.Next
fast = fast.Next.Next
if slow == fast {
return true
}
}
return false
}
medium
A. The function should return false inside the loop when slow != fast
B. The loop condition should check fast.Next != nil to avoid nil pointer dereference
C. The slow pointer should move two steps instead of one
D. The fast pointer should start at head.Next instead of head
Solution
Step 1: Review loop condition
The loop only checks fast != nil, but fast.Next might be nil causing runtime error on fast.Next.Next.
Step 2: Correct loop condition
Adding fast.Next != nil in the loop condition prevents nil pointer dereference when accessing fast.Next.Next.
Final Answer:
The loop condition should check fast.Next != nil to avoid nil pointer dereference -> Option B
Quick Check:
Check fast and fast.Next to avoid errors [OK]
Hint: Always check fast.Next != nil before fast.Next.Next [OK]
Common Mistakes:
Not checking fast.Next leads to runtime panic
Moving slow pointer two steps is incorrect
Returning false inside loop prematurely
5. Given a linked list where the last node points back to the head node, how can you modify the detection function to also return the length of the loop?
hard
A. Return the total number of nodes in the list without checking for loops
B. Count nodes while traversing from head until reaching nil
C. Use a map to store visited nodes and count duplicates
D. After detecting the loop, keep one pointer fixed and move the other until they meet again, counting steps
Solution
Step 1: Detect the loop using two pointers
Use Floyd's cycle detection to find if slow and fast pointers meet inside the loop.
Step 2: Count loop length
Once pointers meet, keep one pointer fixed and move the other pointer forward until it meets the fixed pointer again, counting the steps taken.
Final Answer:
After detecting the loop, keep one pointer fixed and move the other until they meet again, counting steps -> Option D
Quick Check:
Loop length = steps between pointer meetings [OK]
Hint: Count steps between pointer meetings after loop detected [OK]
Common Mistakes:
Counting nodes until nil in circular list causes infinite loop
Using map wastes extra memory
Returning total nodes ignores loop length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.