Bird
0
0
DSA Cprogramming~10 mins

Detect if a Linked List is Circular in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Detect if a Linked List is Circular
Start at head
Check if current is NULL?
YesNo cycle, end
No
Move to current.next
Check if current == head?
YesCycle detected, end
Back to Check if current is NULL?
Start from the head node and move through each node. If you reach NULL, no cycle exists. If you return to the head, a cycle is detected.
Execution Sample
DSA C
struct Node {
  int data;
  struct Node* next;
};

bool isCircular(struct Node* head) {
  struct Node* current = head;
  if (head == NULL) return false;
  while (current != NULL) {
    current = current->next;
    if (current == head) return true;
  }
  return false;
}
This code checks if a linked list loops back to the head, indicating a circular list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start at head1->2->3->NULLcurrent = head (1)1 -> 2 -> 3 -> NULL
2Check current != NULL1->2->3->NULLcurrent = 1 (not NULL)1 -> 2 -> 3 -> NULL
3Move current to current.next1->2->3->NULLcurrent = 21 -> 2 -> 3 -> NULL
4Check current == head?1->2->3->NULLcurrent = 2 (not head)1 -> 2 -> 3 -> NULL
5Check current != NULL1->2->3->NULLcurrent = 2 (not NULL)1 -> 2 -> 3 -> NULL
6Move current to current.next1->2->3->NULLcurrent = 31 -> 2 -> 3 -> NULL
7Check current == head?1->2->3->NULLcurrent = 3 (not head)1 -> 2 -> 3 -> NULL
8Check current != NULL1->2->3->NULLcurrent = 3 (not NULL)1 -> 2 -> 3 -> NULL
9Move current to current.next1->2->3->NULLcurrent = NULL1 -> 2 -> 3 -> NULL
10Check current == head?1->2->3->NULLcurrent = NULL (not head)1 -> 2 -> 3 -> NULL
11Check current != NULL1->2->3->NULLcurrent = NULL (is NULL)1 -> 2 -> 3 -> NULL
12No cycle found, return false1->2->3->NULLcurrent = NULL1 -> 2 -> 3 -> NULL
💡 current reached NULL, so the list is not circular
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
currenthead (1)23NULLNULLNULLNULL
Key Moments - 3 Insights
Why do we check if current == head inside the loop?
Because if current points back to head, it means the list loops and is circular. This is shown in execution_table rows 4, 7, and 10 where the check happens.
Why do we stop when current becomes NULL?
If current is NULL, it means we reached the end of the list without looping back, so no cycle exists. This is shown in execution_table row 11.
What if the list is empty (head is NULL)?
The loop never runs because current starts as NULL, so the function returns false immediately, meaning no cycle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current at Step 6?
A2
B3
CNULL
Dhead (1)
💡 Hint
Check the 'Pointer Changes' column at Step 6 in execution_table.
At which step does the condition current != NULL become false?
AStep 9
BStep 10
CStep 11
DStep 12
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns where current becomes NULL.
If the list was circular, at which step would the function return true?
AWhen current equals head again
BAt the start before the loop
CWhen current becomes NULL
DWhen current is the last node
💡 Hint
Refer to the 'Operation' column checking if current == head inside the loop.
Concept Snapshot
Detect if a Linked List is Circular:
- Start from head node
- Move current pointer through next nodes
- If current becomes NULL, list is not circular
- If current points back to head, list is circular
- Return true if cycle found, else false
Full Transcript
To detect if a linked list is circular, start at the head node and move through each node using a pointer called current. At each step, check if current is NULL. If yes, the list ends and is not circular. If current points back to the head node, a cycle exists and the list is circular. The code moves current to current.next repeatedly until one of these conditions is met. This method ensures we detect loops by seeing if we return to the start node.