Bird
0
0
DSA Cprogramming

Insert at End of Circular Linked List in DSA C

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.
head -> 1 -> 2 -> 3 -> back to 1 (circular)
Insert new node 4 at end:
head -> 1 -> 2 -> 3 -> 4 -> back to 1
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 (circular), insert value 4 at end
Goal: Add node with value 4 at the end so the list becomes 1 -> 2 -> 3 -> 4 (circular)
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: Traverse from head to find last node (node with value 3)
1 -> 2 -> [3] -> back to 1, new node 4 (isolated)
Why: We must find the last node to link it to the new node
Step 3: Set last node's next pointer to new node 4
1 -> 2 -> 3 -> [4] (next=NULL), circle temporarily broken
Why: Link new node after last node to insert at end
Step 4: Set new node's next pointer to head (node 1)
1 -> 2 -> 3 -> 4 -> back to 1 (circular)
Why: Maintain circular link by pointing new node back to head
Result:
1 -> 2 -> 3 -> 4 -> back to 1 (circular)
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for circular linked list
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* createNode(int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

// Insert at end of circular linked list
void insertAtEnd(Node** head, int value) {
    Node* newNode = createNode(value); // create new node
    if (*head == NULL) {
        newNode->next = newNode; // points to itself
        *head = newNode; // head points to new node
        return;
    }
    Node* temp = *head;
    // traverse to last node
    while (temp->next != *head) {
        temp = temp->next; // advance temp
    }
    temp->next = newNode; // last node points to new node
    newNode->next = *head; // new node points to head
}

// Print circular linked list
void printList(Node* head) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("back to %d\n", head->data);
}

int main() {
    Node* head = NULL;
    // Create initial list 1 -> 2 -> 3 (circular)
    insertAtEnd(&head, 1);
    insertAtEnd(&head, 2);
    insertAtEnd(&head, 3);

    printf("Original list:\n");
    printList(head);

    // Insert 4 at end
    insertAtEnd(&head, 4);

    printf("After inserting 4 at end:\n");
    printList(head);

    return 0;
}
Node* newNode = createNode(value); // create new node
create new node to insert
if (*head == NULL) { newNode->next = newNode; *head = newNode; return; }
handle empty list by pointing new node to itself and updating head
while (temp->next != *head) { temp = temp->next; }
traverse to last node which points back to head
temp->next = newNode;
link last node to new node
newNode->next = *head;
link new node back to head to maintain circularity
OutputSuccess
Original list: 1 -> 2 -> 3 -> back to 1 After inserting 4 at end: 1 -> 2 -> 3 -> 4 -> back to 1
Complexity Analysis
Time: O(n) because we traverse all nodes once to find the last node
Space: O(1) because we only create one new node and use a few pointers
vs Alternative: In a singly circular linked list without a tail pointer, inserting at the beginning also requires O(n) traversal to update the last node's next pointer.
Edge Cases
empty list
new node points to itself and becomes head
DSA C
if (*head == NULL) { newNode->next = newNode; *head = newNode; return; }
single node list
new node inserted after the single node and points back to head
DSA C
while (temp->next != *head) { temp = temp->next; }
When to Use This Pattern
When you see a circular linked list and need to add a node at the end, use traversal to find the last node and update pointers to maintain the circle.
Common Mistakes
Mistake: Not updating the new node's next pointer to head, breaking the circular link
Fix: Always set newNode->next = head after inserting at end
Mistake: Not handling empty list case, causing null pointer errors
Fix: Check if head is NULL and handle by pointing new node to itself and updating head
Summary
Adds a new node at the end of a circular linked list, maintaining the circular link.
Use when you want to append data to the end of a circular linked list.
Remember to link the new node back to the head to keep the list circular.