Concept Flow - Circular vs Linear Linked List Key Difference
Start with head node
Traverse nodes one by one
Check next pointer
next == NULL?
End of list
Traverse nodes checking next pointer; linear ends at NULL, circular loops back to head.
Node* head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); // Linear: last->next = NULL // Circular: last->next = head
| Step | Operation | Nodes in List | Pointer Changes | Visual State |
|---|---|---|---|---|
| 1 | Create node 1 | 1 | head -> Node1 | Node1(1) -> NULL |
| 2 | Create node 2 | 2 | Node1.next -> Node2 | Node1(1) -> Node2(2) -> NULL |
| 3 | Create node 3 | 3 | Node2.next -> Node3 | Node1(1) -> Node2(2) -> Node3(3) -> NULL |
| 4 | Linear: last node next = NULL | 3 | Node3.next = NULL | Node1(1) -> Node2(2) -> Node3(3) -> NULL |
| 5 | Circular: last node next = head | 3 | Node3.next = head (Node1) | Node1(1) -> Node2(2) -> Node3(3) -> back to Node1(1) |
| 6 | Traverse linear list | 3 | Traverse stops at Node3.next == NULL | Traversal: 1 -> 2 -> 3 -> NULL |
| 7 | Traverse circular list | 3 | Traverse loops back to head after Node3 | Traversal: 1 -> 2 -> 3 -> 1 -> ... (loops) |
| 8 | End traversal linear | 3 | Stop at NULL | Traversal ends |
| 9 | End traversal circular | 3 | Stop manually to avoid infinite loop | Traversal loops indefinitely without stop |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 | After Step 6 | After Step 7 |
|---|---|---|---|---|---|---|---|---|
| head | NULL | Node1 | Node1 | Node1 | Node1 | Node1 | Node1 | Node1 |
| Node1.next | NULL | NULL | Node2 | Node2 | Node2 | Node2 | Node2 | Node2 |
| Node2.next | NULL | NULL | NULL | Node3 | Node3 | Node3 | Node3 | Node3 |
| Node3.next | NULL | NULL | NULL | NULL | NULL | Node1 | NULL | Node1 |
Circular vs Linear Linked List Key Difference: - Linear list ends with last node pointing to NULL. - Circular list last node points back to head. - Linear traversal stops at NULL. - Circular traversal loops indefinitely unless stopped. - Circular lists are useful for continuous cycles.