Bird
Raised Fist0
DSA C#programming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Delete from End of Doubly Linked List
Start at tail node
↓
Check if list is empty?
| No↓
If only one node
| Yes↓
Set head and tail to null
No↓
Move tail pointer to tail.prev
↓
Set new tail.next to null
↓
Done
Start from the tail node, check if list is empty or has one node, then update tail pointer and remove last node.
Execution Sample
DSA C#
class Node {
  public int data;
  public Node prev, next;
}

// Delete last node from doubly linked list
Deletes the last node from a doubly linked list by updating tail and pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start at tail node (data=30)10 <-> 20 <-> 30tail points to node with data=30ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head tail
2Check if list is empty or single node10 <-> 20 <-> 30No changeSame as step 1
3Move tail pointer to tail.prev (node with data=20)10 <-> 20 <-> 30tail updated to node with data=20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head tail(old) tail(old) ↑ new tail pointer
4Set new tail.next to null (remove last node)10 <-> 20node 20.next = null, node 30 disconnectedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:10│ │ next:20│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head tail Node with data=30 removed
5End of deletion10 <-> 20tail points to node with data=20Same as step 4
6If list had only one node (e.g. 10), set head and tail to nullEmpty listhead = null, tail = nullList is empty: no nodes head = null, tail = null
šŸ’” Deletion ends after updating tail and disconnecting last node or setting list empty if single node
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headNode(10)Node(10)Node(10)Node(10)
tailNode(30)Node(20)Node(20)Node(20)
tail.nextNode(20)Node(30)nullnull
List Size3322
Key Moments - 3 Insights
Why do we move the tail pointer to tail.prev before disconnecting the last node?
Because we need to update the tail to the new last node before removing the old tail. See execution_table step 3 and 4 where tail moves to node 20, then node 30 is disconnected.
What happens if the list has only one node when deleting from end?
We set both head and tail to null to mark the list as empty. This is shown in execution_table step 6.
Why do we set tail.next to null after moving tail pointer?
To remove the reference to the old last node and avoid dangling pointers. This disconnects the removed node from the list, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the tail pointer after step 3?
ANode with data=10
BNode with data=20
CNode with data=30
Dnull
šŸ’” Hint
Check the 'Pointer Changes' column in step 3 of execution_table
At which step does the list size reduce from 3 to 2?
AStep 2
BStep 3
CStep 4
DStep 5
šŸ’” Hint
Look at the 'List Size' row in variable_tracker after each step
If the list had only one node, what would be the final state of head and tail?
ABoth head and tail are null
BBoth point to the single node
Chead points to node, tail is null
Dhead is null, tail points to node
šŸ’” Hint
Refer to execution_table step 6 and key_moments about single node deletion
Concept Snapshot
Delete from End of Doubly Linked List:
- Start at tail node
- If list empty: do nothing
- If single node: set head and tail to null
- Else move tail to tail.prev
- Set new tail.next to null
- Old tail node is removed
- List size decreases by one
Full Transcript
To delete the last node from a doubly linked list, we start at the tail node. If the list is empty, no action is taken. If there is only one node, both head and tail pointers are set to null, making the list empty. Otherwise, we move the tail pointer to the previous node and set the new tail's next pointer to null, disconnecting the old tail node. This removes the last node from the list and reduces the list size by one.

Practice

(1/5)
1. What happens when you delete the last node from a doubly linked list that has multiple nodes?
easy
A. The first node is deleted instead.
B. The head pointer is set to null.
C. The list becomes empty immediately.
D. The second last node becomes the new tail and its next pointer is set to null.

Solution

  1. Step 1: Identify the last node and its previous node

    In a doubly linked list, the last node's previous node is the second last node.
  2. Step 2: Update the second last node's next pointer

    Set the second last node's next pointer to null to remove the last node from the list.
  3. Final Answer:

    The second last node becomes the new tail and its next pointer is set to null. -> Option D
  4. Quick Check:

    Deleting last node updates tail's next to null [OK]
Hint: Last node's previous next pointer must be null after deletion [OK]
Common Mistakes:
  • Setting head to null instead of updating tail
  • Not updating the previous node's next pointer
  • Deleting the first node by mistake
2. Which of the following is the correct C# syntax to delete the last node from a doubly linked list named dll?
easy
A. dll.Head = dll.Head.Next; dll.Head.Previous = null;
B. dll.Tail = dll.Tail.Previous; dll.Tail.Next = null;
C. dll.Tail = null; dll.Head = null;
D. dll.Head.Previous = dll.Tail; dll.Tail.Next = dll.Head;

Solution

  1. Step 1: Move tail pointer to previous node

    Set dll.Tail to dll.Tail.Previous to remove the last node.
  2. Step 2: Set new tail's next pointer to null

    Update dll.Tail.Next to null to mark the end of the list.
  3. Final Answer:

    dll.Tail = dll.Tail.Previous; dll.Tail.Next = null; -> Option B
  4. Quick Check:

    Correct tail update syntax = dll.Tail = dll.Tail.Previous; dll.Tail.Next = null; [OK]
Hint: Tail moves back, next pointer cleared [OK]
Common Mistakes:
  • Updating head instead of tail
  • Not setting tail's next to null
  • Setting both head and tail to null unnecessarily
3. Consider the following C# code that deletes the last node from a doubly linked list. What will be the printed list after deletion?
dll.AddLast(1); dll.AddLast(2); dll.AddLast(3); dll.DeleteFromEnd(); dll.PrintList();
medium
A. 2 -> 3 -> null
B. 1 -> 2 -> 3 -> null
C. 1 -> 2 -> null
D. null

Solution

  1. Step 1: Add nodes 1, 2, 3 to the list

    The list becomes 1 -> 2 -> 3 -> null.
  2. Step 2: Delete the last node (3)

    After deletion, the list becomes 1 -> 2 -> null.
  3. Final Answer:

    1 -> 2 -> null -> Option C
  4. Quick Check:

    Last node removed, list ends at 2 [OK]
Hint: Deleting last removes tail node only [OK]
Common Mistakes:
  • Assuming list remains unchanged
  • Deleting head instead of tail
  • Printing list before deletion
4. The following code attempts to delete the last node from a doubly linked list but causes a runtime error. What is the error?
if (dll.Tail != null) { dll.Tail = dll.Tail.Previous; dll.Tail.Next = null; }
medium
A. NullReferenceException when list has only one node
B. Syntax error due to missing braces
C. The head pointer is not updated
D. The list becomes circular

Solution

  1. Step 1: Check deletion when only one node exists

    If only one node exists, dll.Tail.Previous is null, so dll.Tail becomes null.
  2. Step 2: Accessing dll.Tail.Next causes error

    After setting dll.Tail to null, accessing dll.Tail.Next throws NullReferenceException.
  3. Final Answer:

    NullReferenceException when list has only one node -> Option A
  4. Quick Check:

    Accessing property on null tail causes error [OK]
Hint: Check for null tail before accessing Next [OK]
Common Mistakes:
  • Not handling single-node list case
  • Ignoring null checks before pointer access
  • Assuming head updates automatically
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. After deleting the last node twice, what will be the list and the tail node's value?
hard
A. 10 <-> 20 -> null, Tail = 20
B. 10 <-> 20 <-> 30 -> null, Tail = 30
C. 10 -> null, Tail = 10
D. Empty list, Tail = null

Solution

  1. Step 1: Initial list and first deletion

    List is 10 <-> 20 <-> 30 <-> 40. Deleting last node removes 40, list becomes 10 <-> 20 <-> 30.
  2. Step 2: Second deletion removes 30

    After second deletion, list becomes 10 <-> 20, and tail points to node with value 20.
  3. Final Answer:

    10 <-> 20 -> null, Tail = 20 -> Option A
  4. Quick Check:

    Two deletions remove last two nodes [OK]
Hint: Each deletion removes tail, tail moves back [OK]
Common Mistakes:
  • Deleting only one node instead of two
  • Not updating tail after deletions
  • Confusing head and tail pointers
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