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 one.
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: head moves from node 1 to node 2
null ← 1 ↔ 2 ↔ 3 -> null
     ↑head
Why: We need to update the start to the second node to remove the first
Step 2: set new head's previous 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 will be cleaned up
Result:
null ← 2 ↔ 3 -> null
↑head
Annotated Code
DSA C#
using System;

class Node {
    public int data;
    public Node prev;
    public Node next;
    public Node(int d) { data = d; prev = null; next = null; }
}

class DoublyLinkedList {
    public Node head;

    public void InsertEnd(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        Node curr = head;
        while (curr.next != null) {
            curr = curr.next;
        }
        curr.next = newNode;
        newNode.prev = curr;
    }

    public void DeleteFromBeginning() {
        if (head == null) return; // empty list
        head = head.next; // move head to next node
        if (head != null) {
            head.prev = null; // disconnect previous link
        }
    }

    public void PrintList() {
        Node curr = head;
        while (curr != null) {
            Console.Write(curr.data);
            if (curr.next != null) Console.Write(" ↔ ");
            curr = curr.next;
        }
        Console.WriteLine();
    }
}

class Program {
    static void Main() {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.InsertEnd(1);
        dll.InsertEnd(2);
        dll.InsertEnd(3);
        dll.PrintList();
        dll.DeleteFromBeginning();
        dll.PrintList();
    }
}
head = head.next; // move head to next node
advance head pointer to second node to remove first
if (head != null) { head.prev = null; // disconnect previous link }
remove backward link from new head to old first node
OutputSuccess
1 ↔ 2 ↔ 3 2 ↔ 3
Complexity Analysis
Time: O(1) because we only update pointers at the start without traversing the list
Space: O(1) because no extra space is needed for deletion
vs Alternative: Compared to deleting from the end which requires traversal O(n), deleting from beginning is faster and simpler
Edge Cases
empty list
nothing happens, no error
DSA C#
if (head == null) return; // empty list
single element list
head becomes null, 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 quickly from a doubly linked list, use this pattern to update head and fix links efficiently.
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 by moving the head pointer and fixing links.
Use when you want to remove the front item of a doubly linked list efficiently.
The key is to update head and disconnect the backward link from the new first node.

Practice

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

Solution

  1. Step 1: Understand the role of head pointer

    The head pointer always points to the first node in the doubly linked 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 D
  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 even if list has more nodes
  • 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 in a doubly linked list in C#?
easy
A. head = head.next; head.prev = null;
B. head = head.prev; head.next = null;
C. head.next = null; head.prev = null;
D. head = null; head.next = null;

Solution

  1. Step 1: Move head to next node

    To delete the first node, assign head to head.next to point to the second node.
  2. Step 2: Remove backward link

    Set head.prev to null to disconnect the previous first node.
  3. Final Answer:

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

    Correct syntax updates head and prev pointer [OK]
Hint: Set head to next and prev of new head to null [OK]
Common Mistakes:
  • Setting head.prev instead of head.next
  • Not nullifying prev pointer of new head
  • Setting head to null prematurely
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what will be the list after deleting the first node?
medium
A. 10 <-> 30 <-> null
B. 20 <-> 30 <-> null
C. 30 <-> null
D. null

Solution

  1. Step 1: Identify the first node

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

    Head moves to node with value 20, and its prev pointer is set to null.
  3. Final Answer:

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

    First node removed, list starts at 20 [OK]
Hint: After deletion, head points to second node [OK]
Common Mistakes:
  • Removing wrong node (like 20 or 30)
  • Not updating prev pointer of new head
  • Leaving old head in the list
4. Consider this code snippet to delete the first node from a doubly linked list:
if (head != null) {
    head = head.next;
    head.prev = null;
}
What error can occur here?
medium
A. Logical error: head should not be updated
B. Syntax error due to missing semicolon
C. NullReferenceException if list has only one node
D. No error, code is correct

Solution

  1. Step 1: Analyze when list has one node

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

    Setting head.prev = null when head is null causes NullReferenceException.
  3. Final Answer:

    NullReferenceException if list has only one node -> Option C
  4. Quick Check:

    Accessing property on null causes exception [OK]
Hint: Check if head is null before setting head.prev [OK]
Common Mistakes:
  • Not checking if head is null after moving to next
  • Assuming code always works for single-node list
  • Ignoring runtime exceptions
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> null. After deleting the first node twice, what is the resulting list?
hard
A. 25 <-> 35 <-> null
B. 15 <-> 25 <-> 35 <-> null
C. 35 <-> null
D. null

Solution

  1. Step 1: Delete first node once

    First deletion removes node with value 5; head moves to 15.
  2. Step 2: Delete first node second time

    Second deletion removes node with value 15; head moves to 25.
  3. Final Answer:

    25 <-> 35 <-> null -> Option A
  4. 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 correctly after second deletion
  • Confusing node values order
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