🎉 All 30 days are live — the full DSA-30 course, from Big-O to System Design. See the roadmap →
Day 3 - Linked ListsCircular Linked List

In a circular linked list the tail’s next points back to the head — there’s no NULL anywhere. That one change quietly breaks every traversal habit you just built: the trusty while (ptr != NULL) loop now spins forever. So how do you know when to stop?

Predict first
You run the usual `while (ptr != NULL) { ptr = ptr->next; }` traversal on a circular linked list. What happens?

Basic Operations on a Circular Linked List

We can perform the following basic operation on a Circular Linked List:

  1. Define a Circular Linked List
  2. Insert an Element at the beginning
  3. Insert an Element at the end
  4. Insert an Element after a given position
  5. Delete an Element at the beginning
  6. Delete an Element at the end
  7. Delete an Element after a given position
  8. Traverse and Display the Elements/Nodes

Define a Circular Linked List

  • CODE

Insert an Element at the beginning

  • ALGORITHM
Step 1: if AVAIL = NULL
            Write Overflow
            Go to step 11
        [END OF if]
Step 2: SET newNode = AVAIL
Step 3: SET AVAIL = AVAIL->next
Step 4: SET newNode->data = val
Step 5: SET ptr = start
Step 6: Repeat step 7 while ptr->next != start
Step 7:     ptr = ptr->next
        [END OF LOOP]
Step 8: SET newNode->next = start
Step 9: SET ptr->next = newNode
Step 10: SET start = newNode
Step 11: EXIT
  • CODE

Insert an Element at the end

  • ALGORITHM
    Step 1: if AVAIL = NULL
                Write Overflow
                Go to step 10
            [END OF if]
    Step 2: SET newNode = AVAIL
    Step 3: SET AVAIL = AVAIL->next
    Step 4: SET newNode->data  = val
    Step 5: SET ptr = start
    Step 6: Repeat step 7 while ptr->next != start
    Step 7:     ptr = ptr->next
            [END OF LOOP]
    Step 8: SET ptr->next = newNode
    Step 9: SET newNode->next = start
    Step 10: EXIT
  • CODE

Insert an Element after a given position

  • CODE

Delete an Element at the beginning

  • ALGORITHM
Step 1: if start = NULL
            Write Underflow
            Go to step 5
        [END OF if]
Step 2: SET ptr = start
Step 3: Repeat step 4 while ptr->next != start
Step 4:     SET ptr = ptr->next
        [END OF LOOP]
Step 5: SET ptr->next = start->next
Step 6: free start
Step 7: SET start = ptr->next
Step 8: EXIT
  • CODE

Delete an Element at the end

  • ALGORITHM
Step 1: if start = NULL
            Write Underflow
            Go to step 5
        [END OF if]
Step 2: SET ptr = start
Step 3: Repeat step 4 and 5 while ptr->next != Start
Step 4:     SET prePtr = ptr
Step 5:     SET ptr = ptr->next
        [END OF LOOP]
Step 6: SET prePtr->next = start
Step 7: free ptr
Step 8: EXIT
  • CODE

Delete an Element after a given position

  • ALGORITHM
Step 1: SET ptr = start
Step 2: SET prePtr = ptr
Step 3: Repeat step 4 and 5 while prePtr->data != val
Step 4:         SET prePtr = ptr
Step 5:         SET ptr = ptr->next
        [END OF LOOP]
Step 6: SET prePtr->next = ptr->next
Step 7: if ptr = start
Step 8:         start = prePtr->next
        [END OF if]
Step 9: free ptr
Step 10: EXIT
  • CODE

Traverse and Display the Elements/Nodes

  • CODE

Recall: the stop condition

The whole difference from a singly list is when you stop. Fill in the condition that ends the walk exactly once around:

python · fill in the blanks0/1 hints
def traverse(start):
if start is None:
return
# ??? stop when we loop back to the start
while True:
print(ptr.data)
ptr = ptr.next
if ptr == start: # came full circle — stop here, not at NULL
break

Quick Check

What defines a circular linked list?
Why is a circular linked list a natural fit for a round-robin CPU scheduler?

You’ve now seen the prev pointer (doubly) and the looped tail (circular) separately. Next: circular doubly linked lists, which combine both — every node knows its neighbour on each side, and the list wraps in both directions.

Finished this page?