Bird
Raised Fist0
DSA Cprogramming

Insert at Beginning of Doubly Linked List in DSA C

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node after the tail node
B. To add a new node before the current head node
C. To delete the first node of the list
D. To reverse the entire list

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected both forward and backward, with a head and tail.
  2. Step 2: Identify what inserting at the beginning means

    Inserting at the beginning means adding a new node before the current head node, making it the new head.
  3. Final Answer:

    To add a new node before the current head node -> Option B
  4. Quick Check:

    Insert at beginning = add before head [OK]
Hint: Insert at beginning means new node becomes the head [OK]
Common Mistakes:
  • Confusing insertion at beginning with insertion at end
  • Thinking insertion deletes nodes
  • Mixing up head and tail positions
2. Which of the following is the correct way to update the previous pointer of the old head node when inserting a new node at the beginning in C?
easy
A. new_node->prev = old_head;
B. old_head->next = new_node;
C. new_node->next = old_head->prev;
D. old_head->prev = new_node;

Solution

  1. Step 1: Identify pointer updates when inserting at beginning

    The new node's next points to old head, and old head's prev points back to new node.
  2. Step 2: Choose correct pointer update for old head

    We must set old_head->prev = new_node to link backward to new node.
  3. Final Answer:

    old_head->prev = new_node; -> Option D
  4. Quick Check:

    Old head prev points to new node [OK]
Hint: Old head's prev points to new node after insertion [OK]
Common Mistakes:
  • Setting old_head->next instead of prev
  • Assigning new_node->prev incorrectly
  • Forgetting to update old head's prev pointer
3. What will be the output of the following code snippet after inserting a node with value 10 at the beginning of the doubly linked list?
typedef struct Node {
  int data;
  struct Node* prev;
  struct Node* next;
} Node;

Node* head = NULL;

void insertAtBeginning(int val) {
  Node* new_node = (Node*)malloc(sizeof(Node));
  new_node->data = val;
  new_node->prev = NULL;
  new_node->next = head;
  if (head != NULL) {
    head->prev = new_node;
  }
  head = new_node;
}

int main() {
  insertAtBeginning(20);
  insertAtBeginning(10);
  Node* temp = head;
  while (temp != NULL) {
    printf("%d ", temp->data);
    temp = temp->next;
  }
  return 0;
}
medium
A. 10
B. 20 10
C. 10 20
D. 20

Solution

  1. Step 1: Trace first insertion insertAtBeginning(20)

    List is empty, new node with 20 becomes head: 20 -> NULL
  2. Step 2: Trace second insertion insertAtBeginning(10)

    New node with 10 points next to old head (20), old head prev points to new node, head updated to 10: 10 <-> 20
  3. Final Answer:

    10 20 -> Option C
  4. Quick Check:

    List after insertions = 10 20 [OK]
Hint: New node becomes head, old head shifts right [OK]
Common Mistakes:
  • Printing nodes in reverse order
  • Not updating head pointer
  • Forgetting to update old head's prev pointer
4. Identify the error in the following code snippet for inserting a node at the beginning of a doubly linked list:
void insertAtBeginning(int val) {
  Node* new_node = (Node*)malloc(sizeof(Node));
  new_node->data = val;
  new_node->prev = NULL;
  new_node->next = head;
  if (head != NULL) {
    head->next = new_node;
  }
  head = new_node;
}
medium
A. head->next should be head->prev when updating old head
B. new_node->prev should not be NULL
C. head pointer should not be updated
D. malloc size is incorrect

Solution

  1. Step 1: Check pointer updates for old head

    When inserting at beginning, old head's prev pointer must point to new node, not next.
  2. Step 2: Identify incorrect line

    Code uses head->next = new_node; which wrongly sets old head's next pointer instead of prev.
  3. Final Answer:

    head->next should be head->prev when updating old head -> Option A
  4. Quick Check:

    Old head prev must point to new node [OK]
Hint: Old head prev, not next, points to new node [OK]
Common Mistakes:
  • Updating old head's next pointer instead of prev
  • Not updating head pointer
  • Incorrect malloc size usage
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25. You want to insert a new node with value 1 at the beginning. After insertion, what will be the correct sequence of node values when traversing from head to tail?
medium
A. 1 <-> 5 <-> 15 <-> 25
B. 5 <-> 15 <-> 25 <-> 1
C. 25 <-> 15 <-> 5 <-> 1
D. 1 <-> 15 <-> 5 <-> 25

Solution

  1. Step 1: Understand insertion at beginning

    New node with value 1 becomes new head, pointing next to old head (5).
  2. Step 2: Update pointers and traverse list

    Sequence after insertion: 1 (head) <-> 5 <-> 15 <-> 25 (tail).
  3. Final Answer:

    1 <-> 5 <-> 15 <-> 25 -> Option A
  4. Quick Check:

    New head is 1, followed by old nodes [OK]
Hint: New node becomes head, old head shifts right [OK]
Common Mistakes:
  • Appending new node at end instead of beginning
  • Mixing node order after insertion
  • Not updating head pointer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard