Circular Linked List

In a normal linked list the last node points to null. So when you reach the end, that is it. The list just stops. But what if you want the list to loop back to the start on its own? That is the problem a circular linked list solves. Here the last node points back to the head instead of null. So the whole thing forms a circle.

🎑 A Real-World Picture

Think about a music playlist set on repeat. You play the last song. Then it goes right back to the first song. It never stops at the end. It just keeps going round.

That is exactly a circular linked list. The last node does not point to nothing. It points back to the first node. So you can keep walking the list forever if you want.

Another everyday example is round-robin scheduling. Round-robin means giving each one a fair turn, one after another, in a loop. A computer hands a little time to task A. Then task B. Then task C. Then back to A again. A circular list fits this well. After the last task you land back on the first.

πŸ”— What Makes It Circular

In a singly linked list, each node holds some data and a pointer to the next node. The very last node’s pointer is null. That null is what tells you β€œthis is the end”.

A circular linked list changes just one thing.

  • Every node still holds data and a next pointer, same as before.
  • The last node’s next does not point to null.
  • Instead, the last node’s next points back to the head node.

So there is no real β€œend” anymore. If you keep following next, you go around and around.

Here is the shape of it.

10

20

30

40

See how node 40, the last one, points back to node 10, the head? That arrow looping back is the whole idea.

⚠️ The Big Danger: Infinite Loops

In a normal list you traverse like this. You keep going while the current node is not null. That works because the end is null.

But a circular list never hits null. So if you write that same loop here, you go round and round forever. Your program will freeze. We call this an infinite loop, which means a loop that never stops on its own.

Caution

Never traverse a circular linked list with a while (current != null) check. There is no null to stop at. So the loop runs forever and your program hangs. You must stop when you come back to the head node.

So how do we stop safely? We remember the head. Then we walk forward. The moment we land back on the head again, we know we have gone one full circle. So we stop.

πŸͺœ Steps to Build and Traverse a Circular List

Let us build a small circular list with the values 10, 20, 30, 40. Then we walk it exactly once.

Here is the plan, step by step.

  1. Create the head node with the first value (10).
  2. Create the rest of the nodes (20, 30, 40) and link each one to the next.
  3. Take the last node (40) and point its next back to the head (10). Now it is circular.
  4. To traverse, start at the head and print its value.
  5. Move to the next node and print it. Keep going.
  6. Stop as soon as you arrive back at the head. That means one full circle is done.

That stop condition in step 6 is the key part. It is what saves you from the infinite loop.

πŸ’» Code: Build and Traverse Once

Below we build the circular list. Then we traverse it a single time and print every value. Look at the loop carefully. We use a do-while style so we visit the head first. Then we stop the moment next brings us back to the head.

circular_linked_list.py
# One node: a value and a pointer to the next node
class Node:
def __init__(self, value):
self.data = value
self.next = None
# Walk the circle exactly once and print each value
def traverse(head):
if head is None: # empty list, nothing to do
return
current = head
while True:
print(current.data, end=" ")
current = current.next # move forward
if current == head: # back at head, so stop
break
print()
# Build the four nodes
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)
# Last node points back to head -> now it is circular
head.next.next.next.next = head
traverse(head)

The output of the above code will be:

10 20 30 40

Tip

The do-while pattern fits a circular list perfectly. It prints the current node first. Then it checks the stop condition. So even a list with a single node prints once and then stops cleanly.

⏱️ Time and Space Cost

Walking the list once means touching every node exactly one time. Building the four nodes is just a few steps each. Here is how it adds up.

Operation Time Space
Build the list (n nodes) O(n) O(n)
Traverse once O(n) O(1)
Make it circular (link last to head) O(1) O(1)

Traversal is O(n) in time because you visit each of the n nodes once. It is O(1) in space because you only keep one extra pointer, current. That stays true no matter how big the list gets.

🧩 What You’ve Learned

  • βœ… A circular linked list is one where the last node’s next points back to the head, forming a circle instead of ending at null.
  • βœ… It fits problems that loop naturally, like round-robin scheduling and a music playlist set on repeat.
  • βœ… You make a normal list circular by setting the last node’s next to the head node.
  • βœ… Never use a while (current != null) check here, because there is no null and the loop runs forever.
  • βœ… Traverse safely by remembering the head and stopping the moment you arrive back at it, often with a do-while loop.
  • βœ… Traversing once is O(n) in time and O(1) in extra space.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    In a circular linked list, what does the last node's next pointer point to?

    Why: The last node points back to the head node, which is what forms the circle.

  2. 2

    Why does a while (current != null) loop fail on a circular linked list?

    Why: A circular list never reaches null, so that condition is never false and the loop runs forever.

  3. 3

    What is a safe way to stop while traversing a circular linked list once?

    Why: Remember the head, then stop the moment you come back to it, meaning one full circle is done.

  4. 4

    Which real-world use is a natural fit for a circular linked list?

    Why: Round-robin scheduling loops back to the first task after the last one, which matches a circular list exactly.

πŸš€ What’s Next?

Share & Connect