Bird
0
0
DSA Cprogramming

Insert at End Tail Insert in DSA C

Choose your learning style9 modes available
Mental Model
Add a new item at the very end of a chain of items by moving to the last one and linking the new item after it.
Analogy: Imagine a line of people holding hands. To add a new person at the end, you walk to the last person and hold their hand.
head -> 1 -> 2 -> 3 -> null
                 ↑
               current
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, insert value 4 at end
Goal: Add the value 4 as the last node so the list becomes 1 -> 2 -> 3 -> 4 -> null
Step 1: start at head node with value 1
head -> [1] -> 2 -> 3 -> null
          ↑ current
Why: We begin traversal from the first node to find the end
Step 2: move current to next node with value 2
head -> 1 -> [2] -> 3 -> null
               ↑ current
Why: We keep moving forward to reach the last node
Step 3: move current to next node with value 3
head -> 1 -> 2 -> [3] -> null
                    ↑ current
Why: Current is now at the last node before null
Step 4: create new node with value 4 and link it after current
head -> 1 -> 2 -> 3 -> [4] -> null
                         ↑ new tail
Why: Attach new node at the end by setting last node's next to new node
Result:
1 -> 2 -> 3 -> 4 -> null
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
    int data;
    struct Node* next;
};

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

// Insert at end function
void insertAtEnd(struct Node** head, int value) {
    struct Node* newNode = createNode(value);
    if (*head == NULL) {
        *head = newNode; // empty list, new node is head
        return;
    }
    struct Node* current = *head;
    while (current->next != NULL) {
        current = current->next; // advance to last node
    }
    current->next = newNode; // link new node at end
}

// Print list function
void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("null\n");
}

int main() {
    struct Node* head = createNode(1);
    insertAtEnd(&head, 2);
    insertAtEnd(&head, 3);
    insertAtEnd(&head, 4); // insert value 4 at end
    printList(head);
    return 0;
}
while (current->next != NULL) { current = current->next; // advance to last node }
advance current to last node to find where to insert
current->next = newNode; // link new node at end
attach new node after last node to extend list
if (*head == NULL) { *head = newNode; // empty list, new node is head return; }
handle empty list by making new node the head
OutputSuccess
1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(n) because we traverse all n nodes once to reach the end
Space: O(1) because we only create one new node and use a few pointers
vs Alternative: Compared to inserting at the front which is O(1), inserting at end requires traversal making it O(n)
Edge Cases
empty list
new node becomes the head of the list
DSA C
if (*head == NULL) {
        *head = newNode; // empty list, new node is head
        return;
    }
single element list
new node is linked after the single existing node
DSA C
while (current->next != NULL) {
        current = current->next; // advance to last node
    }
When to Use This Pattern
When you need to add an item at the end of a linked list, use the tail insert pattern by traversing to the last node and linking the new node after it.
Common Mistakes
Mistake: Not handling the empty list case, causing a null pointer dereference
Fix: Check if head is NULL and assign new node as head before traversal
Mistake: Linking new node before reaching the last node, breaking the list
Fix: Traverse fully until current->next is NULL before linking new node
Summary
Adds a new node at the end of a linked list by traversing to the last node and linking the new node.
Use when you want to keep the existing order and append new data at the tail.
The key insight is to find the last node whose next pointer is null and attach the new node there.