3.3Circular Linked List
Traversal and where the loop ends
Traversing a singly linked list ends when temp becomes NULL. A circular linked list has no NULL, so the same loop goes round the list forever. In this lesson you will see where the loop has to stop, how to handle the empty list and how to count the nodes.
- Why a
while (temp != NULL)loop never ends on a circular list - The do-while pattern that starts at head and stops when it gets back to head
- How the empty list is handled, and how the same pattern counts the nodes
1Idea
In a singly linked list the traversal loop is written while (temp != NULL), because the last node's next field is NULL. In a circular list the last node's next field goes back to the first node; temp never becomes NULL and the loop never ends.
In a circular list the stopping point is the starting point: temp starts at head and the loop ends when it gets back to head. But if we write while (temp != head), the condition is false on the very first test, because temp already is head; the loop never runs.
The fix is to test the condition after the body. A do-while loop runs the body once, moves temp on, and only then checks whether it is back at head:
do { // do your work with temp temp = temp->next;} while (temp != head);Since the body runs at least once, an empty list would make temp->data read through NULL. So the empty list is checked before the loop. Python has no do-while; the same pattern is built with while True and an if temp is head: break at the end of the body.
Counting the nodes is the same loop: instead of printing the value you add one to a counter. In a one-node list the body runs once, temp comes back to itself, that is, to head, and the result is 1.
2Step by step
The drawing traverses a circular list holding 12, 7 and 3. Nodes that have been printed turn green. At each step the matching line of code lights up.
- Start. head points to 12 and the last node, 3, points back to 12. There is no NULL in the list.
- The list is not empty.
tempstarts at head, at 12. - 12 is printed and
tempmoves to 7. The condition is tested here for the first time: 7 is not head, so the loop goes on. Output: 12 - 7 is printed and
tempmoves to 3. 3 is not head, so the loop goes on. Output: 12 7 - 3 is printed and
tempfollows the next field of 3 back to 12. Output: 12 7 3 tempequals head again; the condition is false and the loop ends. Every node was printed once.
struct Node { int data; struct Node* next;}; void printList(struct Node* head) { if (head == NULL) return; // empty list struct Node* temp = head; do { printf("%d ", temp->data); temp = temp->next; } while (temp != head); // stop when back at the start printf("\n");} int count(struct Node* head) { if (head == NULL) return 0; int total = 0; struct Node* temp = head; do { total++; temp = temp->next; } while (temp != head); return total;}class Node { int data; Node next; Node(int data) { this.data = data; }} void printList(Node head) { if (head == null) return; // empty list Node temp = head; do { System.out.print(temp.data + " "); temp = temp.next; } while (temp != head); // stop when back at the start System.out.println();} int count(Node head) { if (head == null) return 0; int total = 0; Node temp = head; do { total++; temp = temp.next; } while (temp != head); return total;}class Node: def __init__(self, data): self.data = data self.next = None def print_list(head): if head is None: # empty list return temp = head while True: print(temp.data, end=" ") temp = temp.next if temp is head: # stop when back at the start break print() def count(head): if head is None: return 0 total = 0 temp = head while True: total += 1 temp = temp.next if temp is head: return totalThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each node is visited once. In a list of n nodes the loop body runs n times.
Only the temp pointer is used (plus a counter when counting).
Traversing a singly linked list is O(n) too. The difference is not in time but in the stopping condition: compare with head instead of NULL.
4Common mistake
A common mistake is keeping the condition at the top of the loop, as in a singly linked list, and only replacing NULL with head:
struct Node* temp = head;while (temp != head) { // false on the first test printf("%d ", temp->data); temp = temp->next;}Since temp starts at head, the condition is false on the very first test and nothing is printed. Either use do-while, or handle the first node before the loop and start temp at head->next.
5Try it
What happens if you traverse a circular list with a while (temp != NULL) loop?
What does count return for a one-node circular list?
The answer is B. do-while runs the body once before testing the condition: the counter becomes 1 and temp follows the node's own next field back to head. The condition is false and the result is 1.
Show the answer
The answer is B. do-while runs the body once before testing the condition: the counter becomes 1 and temp follows the node's own next field back to head. The condition is false and the result is 1.
Lessons you mark show up in the curriculum. This is kept on this device only.