Bird
Raised Fist0
DSA Javascriptprogramming

Insert at Beginning of Doubly Linked List in DSA Javascript

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 first book to a shelf where each book knows the one before and after it; you place the new book at the front and connect it to the old first book.
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
↑head
newNode(0)
Why: We need a new node to insert at the beginning
Step 2: Set new node's next pointer to current head (node 1)
newNode(0) β†’ 1 ↔ 2 ↔ 3 β†’ null
↑head
Why: Link new node forward to the old first node
Step 3: Set current head's previous pointer to new node
null ← 0 ↔ 1 ↔ 2 ↔ 3 β†’ null
↑head
Why: Link old first node back to new node
Step 4: Update head pointer to new node
null ← 0 ↔ 1 ↔ 2 ↔ 3 β†’ null
↑head
Why: New node is now the first node in the list
Result:
null ← 0 ↔ 1 ↔ 2 ↔ 3 β†’ null
↑head
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
  }

  insertAtBeginning(value) {
    const newNode = new Node(value); // create new node
    if (this.head === null) {
      this.head = newNode; // empty list, new node is head
      return;
    }
    newNode.next = this.head; // link new node forward to old head
    this.head.prev = newNode; // link old head back to new node
    this.head = newNode; // update head to new node
  }

  printList() {
    let curr = this.head;
    let result = '';
    while (curr !== null) {
      result += curr.value + ' ↔ ';
      curr = curr.next;
    }
    result += 'null';
    console.log(result);
  }
}

// Driver code
const dll = new DoublyLinkedList();
dll.insertAtBeginning(3);
dll.insertAtBeginning(2);
dll.insertAtBeginning(1);
console.log('Before insertion:');
dll.printList();

dll.insertAtBeginning(0);
console.log('After insertion at beginning:');
dll.printList();
const newNode = new Node(value); // create new node
create new node to insert
if (this.head === null) { this.head = newNode; // empty list, new node is head return; }
handle empty list by setting head to new node
newNode.next = this.head; // link new node forward to old head
link new node's next to current head
this.head.prev = newNode; // link old head back to new node
link old head's prev to new node
this.head = newNode; // update head to new node
update head pointer to new node
OutputSuccess
Before insertion: 1 ↔ 2 ↔ 3 ↔ null After insertion at beginning: 0 ↔ 1 ↔ 2 ↔ 3 ↔ null
Complexity Analysis
Time: O(1) because insertion at beginning only changes a few pointers without traversal
Space: O(1) because no extra space is used except the new node
vs Alternative: Compared to inserting at the end which may require traversal O(n), this is faster and simpler
Edge Cases
empty list
new node becomes the head with no next or prev
DSA Javascript
if (this.head === null) {
      this.head = newNode; // empty list, new node is head
      return;
    }
list with one node
new node links to old single node, old node's prev points back
DSA Javascript
newNode.next = this.head; // link new node forward to old head
When to Use This Pattern
When you need to add an element quickly at the start of a list and maintain two-way links, use insert at beginning in a doubly linked list.
Common Mistakes
Mistake: Forgetting to update the old head's prev pointer to the new node
Fix: Always set old head's prev to new node after linking new node's next to old head
Mistake: Not handling empty list case causing null pointer errors
Fix: Check if head is null and set head to new node directly
Summary
Adds a new node at the start of a doubly linked list by adjusting pointers.
Use when you want to quickly insert at the front and keep backward and forward links intact.
The new node's next points to old head, old head's prev points back, then update head to new node.

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 before the current head node
B. To add a new node after the tail node
C. To remove the first node from the list
D. To reverse the entire list

Solution

  1. Step 1: Understand the head node position

    The head node is the first node in the doubly linked list.
  2. Step 2: Insert new node before head

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

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

    Insert at beginning = add before head [OK]
Hint: New node becomes new head in doubly linked list [OK]
Common Mistakes:
  • Confusing insertion at beginning with insertion at end
  • Thinking insertion removes nodes
  • Mixing up head and tail nodes
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 JavaScript?
easy
A. oldHead.prev = newNode;
B. newNode.next = oldHead.prev;
C. newNode.prev = oldHead;
D. oldHead.next = newNode;

Solution

  1. Step 1: Identify the old head's previous pointer

    The old head's previous pointer should point to the new node after insertion.
  2. Step 2: Update old head's prev to new node

    Setting oldHead.prev = newNode links the old head back to the new node.
  3. Final Answer:

    oldHead.prev = newNode; -> Option A
  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 oldHead.next instead of prev
  • Confusing newNode's pointers
  • Assigning newNode.next incorrectly
3. What will be the printed state of the doubly linked list after inserting a node with value 10 at the beginning of the list 5 <-> 15 <-> 20?
medium
A. 5 <-> 15 <-> 20 <-> 10
B. 10 <-> 15 <-> 20 <-> 5
C. 10 <-> 5 <-> 15 <-> 20
D. 5 <-> 10 <-> 15 <-> 20

Solution

  1. Step 1: Insert 10 before current head (5)

    The new node with value 10 becomes the new head, pointing next to 5.
  2. Step 2: Link the rest of the list after 5

    The list continues as 5 <-> 15 <-> 20 unchanged after insertion.
  3. Final Answer:

    10 <-> 5 <-> 15 <-> 20 -> Option C
  4. Quick Check:

    New head 10 followed by old list [OK]
Hint: New node appears first, old list follows [OK]
Common Mistakes:
  • Appending new node at end instead of beginning
  • Swapping nodes incorrectly
  • Ignoring pointer updates
4. Given the following code snippet for inserting at the beginning of a doubly linked list, what is the error?
function insertAtBeginning(head, data) {
  const newNode = { data: data, prev: null, next: head };
  head.prev = newNode;
  head = newNode;
  return head;
}
medium
A. It incorrectly sets newNode.next to null
B. It does not handle empty list when head is null
C. It forgets to update newNode.prev to head
D. It returns the old head instead of new head

Solution

  1. Step 1: Check for empty list case

    If head is null, head.prev causes error because head is null.
  2. Step 2: Missing null check before setting head.prev

    Code should check if head exists before setting head.prev = newNode.
  3. Final Answer:

    It does not handle empty list when head is null -> Option B
  4. Quick Check:

    Null head requires special handling [OK]
Hint: Always check if head is null before accessing its properties [OK]
Common Mistakes:
  • Ignoring empty list case
  • Assuming head is never null
  • Not returning updated head
5. You have a doubly linked list with nodes: 7 <-> 14 <-> 21. You want to insert a new node with value 3 at the beginning. After insertion, what will be the correct sequence of node values when traversing from head to tail and tail to head?
hard
A. Head to tail: 21 <-> 14 <-> 7 <-> 3; Tail to head: 3 <-> 7 <-> 14 <-> 21
B. Head to tail: 7 <-> 14 <-> 21 <-> 3; Tail to head: 3 <-> 21 <-> 14 <-> 7
C. Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 3 <-> 7 <-> 14 <-> 21
D. Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3

Solution

  1. Step 1: Insert 3 before current head 7

    The new node 3 becomes the head, pointing next to 7.
  2. Step 2: Traverse forward and backward

    Forward traversal: 3 -> 7 -> 14 -> 21; backward traversal reverses this order.
  3. Final Answer:

    Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3 -> Option D
  4. Quick Check:

    New head first, tail last, pointers reversed backward [OK]
Hint: Head to tail is forward order; tail to head is reverse [OK]
Common Mistakes:
  • Mixing forward and backward traversal order
  • Appending new node at tail instead of head
  • Incorrect pointer updates causing wrong traversal
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