Bird
0
0
DSA Cprogramming

Insert at Beginning of Doubly Linked List in DSA C

Choose your learning style9 modes available
Mental Model
Add a new node at the start by linking it before the current first node and updating the head pointer.
Analogy: Imagine adding a new book at the front of a shelf where each book is connected to the next and previous one.
null ← 1 ↔ 2 ↔ 3 -> null
↑head
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, insert value 0 at beginning
Goal: Add a new node with value 0 at the start so it becomes the new head
Step 1: Create new node with value 0
null ← 1 ↔ 2 ↔ 3 -> null
newNode(0)
↑head(1)
Why: We need a new node to insert at the beginning
Step 2: Set new node's next to current head (1)
null ← 1 ↔ 2 ↔ 3 -> null
0 -> [next->1]
↑head(1)
Why: Link new node forward to old first node
Step 3: Set current head's prev to new node
null ← 1 ↔ 2 ↔ 3 -> null
0 ↔ 1 ↔ 2 ↔ 3 -> null
↑head(1)
Why: Link old first node backward to new node
Step 4: Update head pointer to new node
null ← 0 ↔ 1 ↔ 2 ↔ 3 -> null
↑head(0)
Why: New node becomes the first node in the list
Result:
null ← 0 ↔ 1 ↔ 2 ↔ 3 -> null
↑head(0)
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

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

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

// Insert at beginning of doubly linked list
void insertAtBeginning(Node** head, int data) {
    Node* newNode = createNode(data); // create new node
    if (*head == NULL) {
        *head = newNode; // empty list, new node is head
        return;
    }
    newNode->next = *head; // link new node forward to old head
    (*head)->prev = newNode; // link old head backward to new node
    *head = newNode; // update head pointer
}

// Print list from head to tail
void printList(Node* head) {
    Node* curr = head;
    while (curr != NULL) {
        printf("%d", curr->data);
        if (curr->next != NULL) printf(" ↔ ");
        curr = curr->next;
    }
    printf(" -> null\n");
}

int main() {
    Node* head = createNode(1);
    head->next = createNode(2);
    head->next->prev = head;
    head->next->next = createNode(3);
    head->next->next->prev = head->next;

    insertAtBeginning(&head, 0);

    printList(head);
    return 0;
}
Node* newNode = createNode(data); // create new node
create a new node to insert
if (*head == NULL) { *head = newNode; return; }
handle empty list by making new node the head
newNode->next = *head; // link new node forward to old head
point new node's next to current head
(*head)->prev = newNode; // link old head backward to new node
point old head's prev to new node
*head = newNode; // update head pointer
update head to new node
OutputSuccess
0 ↔ 1 ↔ 2 ↔ 3 -> null
Complexity Analysis
Time: O(1) because insertion at the beginning only changes a few pointers without traversal
Space: O(1) because only one new node is created regardless of list size
vs Alternative: Compared to inserting at the end which requires traversal O(n), this is faster and simpler
Edge Cases
empty list
new node becomes the head with no next or prev
DSA C
if (*head == NULL) { *head = newNode; return; }
single element list
new node links before the single node, updating head
DSA C
newNode->next = *head; (*head)->prev = newNode;
When to Use This Pattern
When you need to add an element quickly at the start of a doubly linked list, use this pattern to update pointers efficiently.
Common Mistakes
Mistake: Forgetting to update the old head's prev pointer to the new node
Fix: Always set (*head)->prev = newNode after linking newNode->next to *head
Mistake: Not updating the head pointer to the new node
Fix: Assign *head = newNode at the end of insertion
Summary
Inserts a new node at the start of a doubly linked list by adjusting pointers.
Use when you want to add elements quickly at the front without traversing the list.
The key is linking the new node before the current head and updating the head pointer.