📖 Scenario: You are working with a simple linked list that stores numbers. Sometimes, the list might loop back to an earlier node, making it circular. Your task is to check if the linked list is circular or not.
🎯 Goal: Build a program that creates a linked list, sets up a circular link, and then detects if the linked list is circular.
📋 What You'll Learn
Create a linked list with nodes containing integer values
Manually create a circular link in the linked list
Write a method to detect if the linked list is circular
Print true if the list is circular, otherwise false
💡 Why This Matters
🌍 Real World
Circular linked lists are used in real-world applications like music playlists, task scheduling, and network routing where the list loops back to the start.
💼 Career
Understanding how to detect cycles in linked lists is important for software developers working on data structures, debugging memory leaks, or designing efficient algorithms.
Progress0 / 4 steps
1
Create a simple linked list
Create a class called Node with an integer property Value and a Node property Next. Then create three nodes with values 10, 20, and 30. Link them so that the first node points to the second, and the second points to the third.
DSA C#
Hint
Define a Node class with Value and Next. Then create three nodes and link them in order.
2
Make the linked list circular
Create a circular link by setting the Next property of the third node to point back to the first node.
DSA C#
Hint
Set the Next of the last node to the first node to make the list circular.
3
Write a method to detect circular linked list
Write a method called IsCircular that takes a Node called head and returns true if the linked 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, return true. If fast or fast.Next becomes null, return false.
DSA C#
Hint
Use two pointers moving at different speeds to detect a loop. If they meet, the list is circular.
4
Print the result of circular detection
Call the IsCircular method with first as the argument and print the result.
DSA C#
Hint
Call IsCircular(first) and print the result using Console.WriteLine.
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 contains 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 = loop in nodes [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 of the following C# code snippets correctly checks if a linked list is circular using two pointers?
easy
A. if (head == null) return true; else return false;
B. while (slow != null) { slow = slow.Next; if (slow == null) return true; } return false;
C. for (int i = 0; i < length; i++) { if (list[i] == list[length - 1]) return true; } return false;
D. while (fast != null && fast.Next != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) return true; } return false;
Solution
Step 1: Identify Floyd's cycle detection logic
Two pointers move at different speeds: slow moves one step, fast moves two steps.
Step 2: Check for pointer meeting
If slow and fast meet, a cycle exists; otherwise, if fast reaches null, no cycle.
Final Answer:
while (fast != null && fast.Next != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) return true; } return false; -> Option D
Quick Check:
Two-pointer cycle detection = while (fast != null && fast.Next != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) return true; } return false; [OK]
Hint: Use two pointers moving at different speeds [OK]
Common Mistakes:
Checking only one pointer for null
Using for loop on linked list nodes
Returning true when list is empty
3. What will be the output of the following C# code snippet?
class Node { public int Data; public Node Next; public Node(int d) { Data = d; Next = null; } } Node head = new Node(1); head.Next = new Node(2); head.Next.Next = new Node(3); head.Next.Next.Next = head.Next; bool IsCircular(Node node) { Node slow = node, fast = node; while (fast != null && fast.Next != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) return true; } return false; } Console.WriteLine(IsCircular(head));
medium
A. true
B. false
C. Compilation error
D. Runtime exception
Solution
Step 1: Analyze linked list structure
Nodes 1 -> 2 -> 3, then 3 points back to node 2, creating a loop.
Step 2: Trace Floyd's cycle detection
Slow and fast pointers will meet inside the loop, returning true.
Final Answer:
true -> Option A
Quick Check:
Loop detected = true [OK]
Hint: Loop means pointers meet inside list [OK]
Common Mistakes:
Assuming no loop because list looks linear
Confusing pointer equality with data equality
Expecting false because of missing null check
4. Identify the bug in this C# method that tries to detect if a linked list is circular:
bool IsCircular(Node head) { Node slow = head, fast = head; while (fast != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) return true; } return false; }
medium
A. Missing check for fast.Next being null before accessing fast.Next.Next
B. Slow pointer should move two steps instead of one
C. Should return false inside the loop instead of after
D. Head node should be initialized to null
Solution
Step 1: Review pointer movement and null checks
fast moves two steps: fast = fast.Next.Next; must check fast.Next != null to avoid null reference.
Step 2: Identify missing null check
Loop condition only checks fast != null, but fast.Next could be null causing runtime error.
Final Answer:
Missing check for fast.Next being null before accessing fast.Next.Next -> Option A
Quick Check:
Check fast.Next != null before fast.Next.Next [OK]
Hint: Always check fast and fast.Next for null before moving fast [OK]
Common Mistakes:
Ignoring fast.Next null check
Moving slow pointer two steps incorrectly
Returning false too early inside loop
5. Given a linked list where some nodes may form multiple loops, which approach correctly detects if the list is circular and why?
public bool IsCircular(Node head) { Node slow = head, fast = head; while (fast != null && fast.Next != null) { slow = slow.Next; fast = fast.Next.Next; if (slow == fast) return true; } return false; }
hard
A. It fails because it only detects the first loop and misses others
B. Floyd's cycle detection works because fast pointer will eventually meet slow pointer in any loop
C. It requires extra memory to store visited nodes to detect multiple loops