Bird
Raised Fist0
DSA C#programming~10 mins

Delete from Beginning of Doubly Linked List in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head node
↓
Check if list is empty?
↓
Nothing to delete
↓
Done
↓
Delete old head
↓
Done
Start at the head node, check if list is empty. If not, move head to next node, update new head's prev pointer to null, then delete old head.
Execution Sample
DSA C#
class Node {
  public int data;
  public Node prev, next;
}

// Delete from beginning
void DeleteFromBeginning() {
  if (head == null) return;
  head = head.next;
  if (head != null) head.prev = null;
}
Deletes the first node of a doubly linked list by moving head to next and updating pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with list: 10 <-> 20 <-> 3010 <-> 20 <-> 30head → node with data 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if list is empty10 <-> 20 <-> 30No changeSame as Step 1
3Move head to next node (20)20 <-> 30head → node with data 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ prev:āˆ… │←── │ prev:10│←── │ data:30│ │ next:20│──→ │ next:30│──→ │ prev:20│ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ next:āˆ… │ (old head) head ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
4Update new head's prev to null20 <-> 30head.prev = nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ prev:āˆ… │←── │ prev:āˆ… │←── │ data:30│ │ next:20│──→ │ next:30│──→ │ prev:20│ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ next:āˆ… │ (old head) head ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
5Delete old head (10) - no references now20 <-> 30old head removedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6End of operation20 <-> 30No changeFinal list state as above
šŸ’” head moved to next node; old head removed; new head prev pointer set to null
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headnode with data 10node with data 20node with data 20node with data 20
head.prevnullnode with data 10nullnull
old headnode with data 10node with data 10node with data 10deleted
Key Moments - 3 Insights
Why do we set the new head's prev pointer to null after moving head?
Because the new head is now the first node, it should not point back to any previous node. This is shown in Step 4 of the execution_table where head.prev changes from pointing to old head to null.
What happens if the list is empty when we try to delete from beginning?
The function checks if head is null in Step 2. If yes, it returns immediately without changes, so no deletion happens.
Why do we still show the old head node after moving head to next?
Because the old head node still exists in memory until no references point to it. After updating head.prev to null, the old head has no references and can be deleted (Step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what does the head pointer point to?
ANode with data 20
BNode with data 10
CNode with data 30
DNull
šŸ’” Hint
Check the 'Pointer Changes' column in Step 3 of execution_table.
At which step does the new head's prev pointer get set to null?
AStep 2
BStep 4
CStep 3
DStep 5
šŸ’” Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table.
If the list had only one node, what would be the final state after deletion?
Ahead points to a new node with data 0
Bhead points to the same node
Chead points to null (empty list)
Dhead.prev points to old node
šŸ’” Hint
Think about what happens when head.next is null after moving head.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == null)
- Move head to head.next
- If new head exists, set head.prev = null
- Old head node is removed (no references)
- List size decreases by one
- Handles empty list safely
Full Transcript
To delete the first node in a doubly linked list, start at the head. If the list is empty, do nothing. Otherwise, move the head pointer to the next node. Then set the new head's prev pointer to null because it is now the first node. The old head node is no longer referenced and can be deleted. This operation reduces the list size by one and maintains proper links. If the list had only one node, after deletion the list becomes empty with head pointing to null.

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