📖 Scenario: Imagine you have a chain of connected train cars. Normally, the last car is not connected back to the first car. But sometimes, the last car might connect back to an earlier car, making a loop. This is called a circular chain.In programming, a linked list is like this chain of train cars. We want to check if the linked list forms a loop (circular) or not.
🎯 Goal: You will build a simple linked list and write code to check if it is circular (has a loop) or not.
📋 What You'll Learn
Create a linked list with exactly 5 nodes with values 10, 20, 30, 40, 50
Create a boolean variable called isCircular to store the result
Write a function called checkCircular that returns true if the linked list is circular, false otherwise
Print "Circular" if the list is circular, else print "Not Circular"
💡 Why This Matters
🌍 Real World
Detecting loops in linked lists is important in many software systems to avoid infinite loops and crashes, such as in network routing, memory management, and data processing.
💼 Career
Understanding how to detect circular linked lists is a common interview question and a fundamental skill for software developers working with data structures.
Progress0 / 4 steps
1
Create a linked list with 5 nodes
Create a linked list with 5 nodes having values 10, 20, 30, 40, and 50. Use a struct called Node with an integer data and a pointer next. Create a pointer head pointing to the first node.
DSA C++
Hint
Think of each node as a train car. Create each car and connect it to the next.
2
Add a boolean variable to store circular check result
Create a boolean variable called isCircular and set it to false initially. This will store if the linked list is circular or not.
DSA C++
Hint
Use bool isCircular = false; to create the variable.
3
Write a function to check if the linked list is circular
Write a function called checkCircular that takes a pointer head to the linked list and returns true if the list is circular, otherwise false. Use two pointers called slow and fast. Move slow by one step and fast by two steps. If they meet, the list is circular. If fast or fast->next becomes nullptr, the list is not circular.
DSA C++
Hint
Use Floyd's cycle detection method with two pointers moving at different speeds.
4
Call the function and print if the list is circular
Call the function checkCircular with head and store the result in isCircular. Then print "Circular" if isCircular is true, otherwise print "Not Circular".
DSA C++
Hint
Call isCircular = checkCircular(head); then use if to print the correct message.
Practice
(1/5)
1. What does it mean if a linked list is circular?
easy
A. The linked list nodes are sorted in ascending order
B. The linked list has no nodes
C. The last node points back to one of the previous nodes, forming a loop
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 C
Quick Check:
Circular linked list = last node points back [OK]
Hint: Circular means last node links back to earlier node [OK]
Common Mistakes:
Thinking circular means sorted list
Confusing empty list with circular list
Assuming single node list is always circular
2. Which of the following C++ code snippets correctly checks if a linked list is circular using a slow and fast pointer approach?
C. for(int i=0; i<10; i++) { if(slow == fast) return true; slow = slow->next; fast = fast->next; } return false;
D. if(head == nullptr) return true; else return false;
Solution
Step 1: Understand slow and fast pointer method
Two pointers move at different speeds; if they meet, a loop exists.
Step 2: Analyze code correctness
while(fast != nullptr && fast->next != nullptr) { slow = slow->next; fast = fast->next->next; if(slow == fast) return true; } return false; correctly moves slow by one and fast by two nodes, checking for equality inside the loop.
A. The loop condition should check fast->next != nullptr to avoid null pointer dereference
B. The slow pointer should move two steps instead of one
C. The function should return false inside the loop
D. The function should initialize slow and fast to nullptr
Solution
Step 1: Check loop condition safety
fast->next->next is accessed, so fast->next must not be nullptr to avoid crash.
Step 2: Correct loop condition
Change while(fast != nullptr) to while(fast != nullptr && fast->next != nullptr) to prevent null pointer dereference.
Final Answer:
The loop condition should check fast->next != nullptr to avoid null pointer dereference -> Option A
Quick Check:
Check fast and fast->next to avoid crash [OK]
Hint: Always check fast->next before fast->next->next [OK]
Common Mistakes:
Not checking fast->next leads to runtime error
Moving slow pointer incorrectly
Returning false too early
5. You have a singly linked list where the last node might point back to any node, making it circular. How would you modify the list to remove the circular link and make it linear again?
hard
A. Set head to nullptr to clear the list
B. Delete all nodes after the head node
C. Reverse the linked list to break the loop
D. Detect the loop using slow and fast pointers, then find the node before the loop start and set its next to nullptr
Solution
Step 1: Detect loop using slow and fast pointers
Use two pointers moving at different speeds; if they meet, a loop exists.
Step 2: Find the node before loop start and break the loop
Reset one pointer to head, move both one step until they meet; the node before meeting point is loop end. Set its next to nullptr.
Final Answer:
Detect the loop using slow and fast pointers, then find the node before the loop start and set its next to nullptr -> Option D
Quick Check:
Find loop start, break link to remove circular [OK]
Hint: Find loop start, set previous node's next to nullptr [OK]
Common Mistakes:
Deleting nodes unnecessarily
Reversing list does not remove loop
Setting head to nullptr loses entire list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.