Bird
Raised Fist0
DSA C++programming

Delete from Beginning of Doubly Linked List in DSA C++

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; the second carriage becomes the new front and no longer points back to the removed carriage.
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 so it starts at node 2
Step 1: store current head (node 1) to delete
null ← [1] ↔ 2 ↔ 3 -> null
↑head
Why: We need to remember the node to delete it safely
Step 2: move head pointer to next node (node 2)
null ← 1 ↔ [2] ↔ 3 -> null
↑head
Why: The second node becomes the new start of the list
Step 3: set new head's previous pointer to null
null ← [2] ↔ 3 -> null
↑head
Why: The new first node should not point back to the deleted node
Step 4: delete old head node (node 1)
null ← 2 ↔ 3 -> null
↑head
Why: Remove the old first node to free memory and complete deletion
Result:
null ← 2 ↔ 3 -> null
↑head
Annotated Code
DSA C++
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* prev;
    Node* next;
    Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};

class DoublyLinkedList {
public:
    Node* head;
    DoublyLinkedList() : head(nullptr) {}

    void append(int val) {
        Node* newNode = new Node(val);
        if (!head) {
            head = newNode;
            return;
        }
        Node* temp = head;
        while (temp->next) {
            temp = temp->next;
        }
        temp->next = newNode;
        newNode->prev = temp;
    }

    void deleteFromBeginning() {
        if (!head) return; // empty list
        Node* toDelete = head; // store node to delete
        head = head->next; // move head to next node
        if (head) {
            head->prev = nullptr; // fix prev pointer
        }
        delete toDelete; // free memory
    }

    void printList() {
        Node* temp = head;
        while (temp) {
            cout << temp->data;
            if (temp->next) cout << " ↔ ";
            temp = temp->next;
        }
        cout << " -> null" << endl;
    }
};

int main() {
    DoublyLinkedList dll;
    dll.append(1);
    dll.append(2);
    dll.append(3);
    dll.printList();
    dll.deleteFromBeginning();
    dll.printList();
    return 0;
}
if (!head) return; // empty list
check if list is empty to avoid errors
Node* toDelete = head; // store node to delete
keep reference to current first node for deletion
head = head->next; // move head to next node
advance head pointer to second node
if (head) { head->prev = nullptr; // fix prev pointer }
remove backward link from new head to old node
delete toDelete; // free memory
remove old first node from memory
OutputSuccess
1 ↔ 2 ↔ 3 -> null 2 ↔ 3 -> null
Complexity Analysis
Time: O(1) because deletion from the beginning only changes a few pointers without traversal
Space: O(1) because no extra space is needed besides temporary pointer
vs Alternative: Compared to deleting from the end which requires traversal O(n), deleting from beginning is faster and simpler
Edge Cases
empty list
function returns immediately without error
DSA C++
if (!head) return; // empty list
single element list
head becomes null after deletion, list becomes empty
DSA C++
head = head->next; // move head to next node
When to Use This Pattern
When you need to remove the first item in a doubly linked list quickly, use this pattern to update head and fix links safely.
Common Mistakes
Mistake: Not setting the new head's prev pointer to null after deletion
Fix: Always set head->prev = nullptr after moving head to next node
Mistake: Trying to delete from an empty list without checking
Fix: Add a check if head is null before deletion
Summary
Deletes the first node of a doubly linked list by moving the head pointer and fixing links.
Use when you want to remove the front element efficiently in a doubly linked list.
The key is to update the head pointer and clear the backward link from the new head.

Practice

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

Solution

  1. Step 1: Understand the head pointer role

    The head points to the first node in the doubly linked list.
  2. Step 2: After deletion, update the head pointer

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

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

    Head points to new first node after deletion [OK]
Hint: Head always moves to next node after deleting first [OK]
Common Mistakes:
  • Not updating head pointer after deletion
  • Setting head to null without checking list size
  • Leaving head pointing to deleted node
2. Which of the following is the correct syntax to delete the first node in a doubly linked list in C++?
easy
A. head = head->next; delete head->prev; head->prev = nullptr;
B. Node* temp = head; head = head->next; head->prev = nullptr; delete temp;
C. head->prev = nullptr; delete head; head = head->next;
D. delete head; head = head->next; head->prev = nullptr;

Solution

  1. Step 1: Store current head in temp

    We save the current first node in a temporary pointer to delete it safely after updating head.
  2. Step 2: Update head and fix previous pointer

    Set head to the next node, then set head->prev to nullptr to detach the old first node.
  3. Step 3: Delete the old first node

    Finally, delete the node stored in temp to free memory.
  4. Final Answer:

    Node* temp = head; head = head->next; head->prev = nullptr; delete temp; -> Option B
  5. Quick Check:

    Proper order: save, update, fix pointers, delete [OK]
Hint: Save head first, update pointers, then delete saved node [OK]
Common Mistakes:
  • Deleting head before updating pointers
  • Not setting head->prev to nullptr
  • Accessing head->prev when head is nullptr
3. What will be the output of the following code snippet after deleting the first node?
struct Node {
  int data;
  Node* prev;
  Node* next;
};

Node* head = new Node{1, nullptr, nullptr};
Node* second = new Node{2, head, nullptr};
head->next = second;

// Delete from beginning
Node* temp = head;
head = head->next;
head->prev = nullptr;
delete temp;

// Print list
Node* curr = head;
while(curr != nullptr) {
  std::cout << curr->data << " ";
  curr = curr->next;
}
medium
A. 1 2
B. No output (runtime error)
C. 1
D. 2

Solution

  1. Step 1: Initial list setup

    List has two nodes: 1 (head) -> 2. Head points to node with data 1.
  2. Step 2: Delete first node and update pointers

    Head moves to second node (data 2), and its prev is set to nullptr. The first node is deleted.
  3. Step 3: Print remaining list

    Only node with data 2 remains, so output is "2".
  4. Final Answer:

    2 -> Option D
  5. Quick Check:

    After deletion, list starts at 2 [OK]
Hint: After deleting head, print starts from new head [OK]
Common Mistakes:
  • Printing deleted node's data
  • Not updating head->prev to nullptr
  • Assuming both nodes print after deletion
4. Identify the error in the following code that deletes the first node of a doubly linked list:
void deleteBeginning(Node*& head) {
  if (head == nullptr) return;
  delete head;
  head = head->next;
  if (head != nullptr) head->prev = nullptr;
}
medium
A. Deleting head before updating head pointer causes undefined behavior
B. Missing check if head->next is nullptr
C. head->prev should be set to head->next
D. No error, code works fine

Solution

  1. Step 1: Analyze deletion order

    The code deletes head first, then tries to access head->next, which is invalid after deletion.
  2. Step 2: Correct order to avoid undefined behavior

    First save head->next in a temp pointer, update head, then delete old node.
  3. Final Answer:

    Deleting head before updating head pointer causes undefined behavior -> Option A
  4. Quick Check:

    Accessing deleted memory causes error [OK]
Hint: Always update pointers before deleting nodes [OK]
Common Mistakes:
  • Deleting node before updating pointers
  • Accessing deleted node's members
  • Not checking for empty list
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. After deleting the first node twice from the beginning, what will be the printed list?
hard
A. 30 <-> 40
B. 20 <-> 30 <-> 40
C. 10 <-> 20 <-> 30 <-> 40
D. 40

Solution

  1. Step 1: Initial list nodes

    List is 10 <-> 20 <-> 30 <-> 40, head points to 10.
  2. Step 2: First deletion

    Delete node 10, head moves to 20, set head->prev = nullptr. List now: 20 <-> 30 <-> 40.
  3. Step 3: Second deletion

    Delete node 20, head moves to 30, set head->prev = nullptr. List now: 30 <-> 40.
  4. Final Answer:

    30 <-> 40 -> Option A
  5. Quick Check:

    Two deletions remove first two nodes [OK]
Hint: Each deletion moves head to next node [OK]
Common Mistakes:
  • Deleting only once instead of twice
  • Not updating head->prev after second deletion
  • Printing old list without deletions
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