0
0
DSA Pythonprogramming

Insert at End of Circular Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
A circular linked list connects the last node back to the first, forming a loop. Inserting at the end means adding a new node just before the first node and updating the last node to point to this new node.
Analogy: Imagine people sitting in a circle holding hands. Adding a new person at the end means placing them just before the first person and making sure the last person now holds the new person's hand, keeping the circle unbroken.
head -> 1 -> 2 -> 3 -> back to head (1)

Visual: 1 -> 2 -> 3 ->
          ↑         ↓
          ←---------
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 (circular), insert value 4 at end
Goal: Add a new node with value 4 at the end, maintaining the circular link
Step 1: Create new node with value 4
1 -> 2 -> 3 -> back to 1, new node 4 (isolated)
Why: We need a new node to insert at the end
Step 2: Find last node (node with value 3) by traversing from head
1 -> 2 -> [3] -> back to 1, new node 4 (isolated)
Why: To insert at end, we must find the current last node
Step 3: Set last node's next pointer from 1 to new node 4
1 -> 2 -> 3 -> 4 (new), 4 -> back to 1
Why: Link last node to new node to add it at the end
Step 4: Set new node 4's next pointer to head (node 1)
1 -> 2 -> 3 -> 4 -> back to 1

Visual loop maintained
Why: Maintain circular link by pointing new node back to head
Result:
1 -> 2 -> 3 -> 4 -> back to 1
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularLinkedList:
    def __init__(self):
        self.head = None

    def insert_at_end(self, data):
        new_node = Node(data)  # create new node
        if not self.head:
            new_node.next = new_node  # points to itself
            self.head = new_node
            return
        curr = self.head
        while curr.next != self.head:  # find last node
            curr = curr.next
        curr.next = new_node  # last node points to new node
        new_node.next = self.head  # new node points to head

    def __str__(self):
        if not self.head:
            return "empty"
        result = []
        curr = self.head
        while True:
            result.append(str(curr.data))
            curr = curr.next
            if curr == self.head:
                break
        return " -> ".join(result) + " -> back to " + str(self.head.data)

# Driver code
cll = CircularLinkedList()
cll.insert_at_end(1)
cll.insert_at_end(2)
cll.insert_at_end(3)
cll.insert_at_end(4)
print(cll)
new_node = Node(data) # create new node
create the new node to insert
if not self.head: new_node.next = new_node # points to itself self.head = new_node return
handle empty list by pointing new node to itself and setting head
while curr.next != self.head: # find last node curr = curr.next
traverse to find the last node which points back to head
curr.next = new_node # last node points to new node
link last node to new node to insert at end
new_node.next = self.head # new node points to head
maintain circular link by pointing new node back to head
OutputSuccess
1 -> 2 -> 3 -> 4 -> back to 1
Complexity Analysis
Time: O(n) because we traverse the list to find the last node before insertion
Space: O(1) because we only create one new node and use a few pointers
vs Alternative: Compared to a singly linked list, circular linked list requires careful pointer updates to maintain the loop, but insertion at end is similar in time cost
Edge Cases
empty list
new node points to itself and becomes head, forming a single-node circular list
DSA Python
if not self.head:
    new_node.next = new_node  # points to itself
    self.head = new_node
    return
single node list
new node inserted after the single node, and circular link maintained
DSA Python
while curr.next != self.head:  # find last node
    curr = curr.next
When to Use This Pattern
When you see a problem requiring insertion at the end of a circular linked list, look for traversal to the last node and pointer updates that maintain the loop.
Common Mistakes
Mistake: Not updating the new node's next pointer to head, breaking the circular link
Fix: Always set new_node.next = self.head after insertion
Mistake: Not handling empty list case, causing errors when head is None
Fix: Check if head is None and initialize the list properly by pointing new node to itself
Summary
Adds a new node at the end of a circular linked list while keeping the loop intact.
Use when you need to append data to a circular linked list without breaking its circular nature.
The key is to find the last node and update pointers so the new node points back to the head.