Bird
Raised Fist0
DSA Javascriptprogramming

Delete from Beginning of Doubly Linked List in DSA Javascript

Choose your learning style10 modes available
Mental Model
Removing the first item means moving the start pointer to the next item and fixing links so the list stays connected.
Analogy: Imagine a train where you remove the first carriage and connect the engine directly to the second carriage so the train stays whole.
null ← 1 ↔ 2 ↔ 3 -> null
↑ head
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, delete from beginning
Goal: Remove the first node and update the list to start from the second node
Step 1: head moves from node 1 to node 2
null ← 1 ↔ 2 ↔ 3 -> null
     ↑ old head
null ← 2 ↔ 3 -> null
↑ head
Why: We want to remove the first node, so head must point to the second node
Step 2: set new head's prev pointer to null
null ← 2 ↔ 3 -> null
↑ head
Why: The new first node should not point back to the removed node
Step 3: old first node is disconnected and ready for garbage collection
null ← 2 ↔ 3 -> null
↑ head
Why: The removed node is no longer linked and can be cleaned up
Result:
null ← 2 ↔ 3 -> null
↑ head
Annotated Code
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let curr = this.head;
    while (curr.next) {
      curr = curr.next;
    }
    curr.next = newNode;
    newNode.prev = curr;
  }

  deleteFromBeginning() {
    if (!this.head) return; // empty list
    this.head = this.head.next; // move head to next node
    if (this.head) {
      this.head.prev = null; // disconnect prev pointer
    }
  }

  printList() {
    let curr = this.head;
    let result = '';
    while (curr) {
      result += curr.data + (curr.next ? ' ↔ ' : ' -> null');
      curr = curr.next;
    }
    console.log(result);
  }
}

// Driver code
const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.printList();
dll.deleteFromBeginning();
dll.printList();
this.head = this.head.next; // move head to next node
advance head pointer to remove first node
if (this.head) { this.head.prev = null; // disconnect prev pointer }
fix new head's prev pointer to null to keep list integrity
OutputSuccess
1 ↔ 2 ↔ 3 -> null 2 ↔ 3 -> null
Complexity Analysis
Time: O(1) because we only change a few pointers at the start without traversing the list
Space: O(1) because no extra space is needed, just pointer updates
vs Alternative: Compared to deleting from the end which requires traversal, deleting from beginning is faster and simpler
Edge Cases
empty list
nothing changes, no error occurs
DSA Javascript
if (!this.head) return; // empty list
list with one node
head becomes null, list becomes empty
DSA Javascript
this.head = this.head.next; // move head to next node
When to Use This Pattern
When you need to remove the first item quickly from a list and keep the rest connected, use delete from beginning in doubly linked list.
Common Mistakes
Mistake: Not setting the new head's prev pointer to null after deletion
Fix: Always set head.prev = null after moving head to next node
Mistake: Trying to delete from an empty list without checking
Fix: Check if head is null before deleting to avoid errors
Summary
Deletes the first node of a doubly linked list by moving the head pointer and fixing links.
Use when you want to quickly remove the first element from a doubly linked list.
The key is to move head forward and disconnect the old first node's backward link.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It remains unchanged
B. It moves to the second node in the list
C. It points to the last node
D. It becomes null immediately

Solution

  1. Step 1: Understand the head pointer role

    The head pointer always points to the first node in the list.
  2. Step 2: Effect of deleting the first node

    When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.
  3. Final Answer:

    It moves to the second node in the list -> Option B
  4. Quick Check:

    Head points to new first node after deletion [OK]
Hint: Head always points to the first node after deletion [OK]
Common Mistakes:
  • Assuming head becomes null without checking list length
  • Not updating head pointer after deletion
  • Pointing head to last node mistakenly
2. Which of the following is the correct syntax to delete the first node from a doubly linked list in JavaScript?
easy
A. head.prev = null; head = head.next;
B. head = null; head.next = null;
C. head.next = null; head = head.prev;
D. head = head.next; head.prev = null;

Solution

  1. Step 1: Move head to next node

    Assign head to head.next to remove the first node reference.
  2. Step 2: Update new head's prev pointer

    Set head.prev to null to disconnect backward link from old first node.
  3. Final Answer:

    head = head.next; head.prev = null; -> Option D
  4. Quick Check:

    Correct syntax updates head and prev pointer [OK]
Hint: Update head first, then set new head.prev to null [OK]
Common Mistakes:
  • Setting head.prev before moving head
  • Setting head.next to null incorrectly
  • Nullifying head before updating pointers
3. Consider the doubly linked list: 10 <-> 20 <-> 30 <-> null. After deleting the first node, what will be the new list?
medium
A. 30 <-> null
B. 10 <-> 30 <-> null
C. 20 <-> 30 <-> null
D. null

Solution

  1. Step 1: Identify the first node

    The first node is 10, which will be removed.
  2. Step 2: Update head to second node

    Head moves to 20, and 20.prev is set to null.
  3. Final Answer:

    20 <-> 30 <-> null -> Option C
  4. Quick Check:

    First node removed, list starts at 20 [OK]
Hint: After deletion, list starts from second node [OK]
Common Mistakes:
  • Leaving old head in list
  • Not updating prev pointer of new head
  • Removing wrong node
4. Given this code snippet to delete the first node, what is the error?
if (head !== null) {
  head = head.next;
  head.prev = null;
}
medium
A. head.prev assignment causes error if list has one node
B. No error, code works fine
C. head.next should be assigned to null before head update
D. head should be set to null before updating prev

Solution

  1. Step 1: Check list with one node

    If list has one node, head.next is null after head = head.next.
  2. Step 2: Accessing head.prev causes error

    head is now null, so head.prev causes runtime error (cannot read property 'prev' of null).
  3. Final Answer:

    head.prev assignment causes error if list has one node -> Option A
  4. Quick Check:

    Null head causes error on prev access [OK]
Hint: Check if head is null before accessing head.prev [OK]
Common Mistakes:
  • Not handling single-node list case
  • Accessing properties of null head
  • Assuming list always has multiple nodes
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null. Write a function to delete the first node and return the updated list head. What will be the list after deletion?
hard
A. 15 <-> 25 <-> 35 <-> null
B. 5 <-> 25 <-> 35 <-> null
C. 25 <-> 35 <-> null
D. null

Solution

  1. Step 1: Move head to next node

    Head moves from 5 to 15, the second node.
  2. Step 2: Set new head's prev to null

    Set 15.prev = null to disconnect from old first node.
  3. Step 3: Return updated head

    Return the new head pointing to 15.
  4. Final Answer:

    15 <-> 25 <-> 35 <-> null -> Option A
  5. Quick Check:

    First node removed, list starts at 15 [OK]
Hint: Update head and set new head.prev to null [OK]
Common Mistakes:
  • Not updating prev pointer of new head
  • Returning old head instead of new head
  • Deleting wrong node or skipping nodes
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