Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Delete from End of Doubly Linked List in DSA Javascript - 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
↓
Delete old tail node
↓
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 Javascript
function deleteFromEnd() {
  if (!tail) return;
  if (head === tail) {
    head = tail = null;
  } else {
    tail = tail.prev;
    tail.next = null;
  }
}
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: 40)10 <-> 20 <-> 30 <-> 40tail points to node with data 40β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ data:10β”‚ ⇄ β”‚ data:20β”‚ ⇄ β”‚ data:30β”‚ ⇄ β”‚ data:40β”‚ β”‚ prev:βˆ… β”‚ β”‚ prev:10β”‚ β”‚ prev:20β”‚ β”‚ prev:30β”‚ β”‚ next:20β”‚ β”‚ next:30β”‚ β”‚ next:40β”‚ β”‚ next:βˆ… β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ head tail
2Check if list is empty10 <-> 20 <-> 30 <-> 40No changeSame as step 1
3Check if only one node10 <-> 20 <-> 30 <-> 40No changeSame as step 1
4Move tail pointer to tail.prev (node with data 30)10 <-> 20 <-> 30 <-> 40tail moves from 40 to 30β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ data:10β”‚ ⇄ β”‚ data:20β”‚ ⇄ β”‚ data:30β”‚ ⇄ β”‚ data:40β”‚ β”‚ prev:βˆ… β”‚ β”‚ prev:10β”‚ β”‚ prev:20β”‚ β”‚ prev:30β”‚ β”‚ next:20β”‚ β”‚ next:30β”‚ β”‚ next:40β”‚ β”‚ next:βˆ… β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ head tailβ†’old tail
5Set new tail.next to null (break link to old tail)10 <-> 20 <-> 30node 30.next = nullβ”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ data:10β”‚ ⇄ β”‚ data:20β”‚ ⇄ β”‚ data:30β”‚ β”‚ prev:βˆ… β”‚ β”‚ prev:10β”‚ β”‚ prev:20β”‚ β”‚ next:20β”‚ β”‚ next:30β”‚ β”‚ next:βˆ… β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ head tail
6Delete old tail node (data 40)10 <-> 20 <-> 30old tail node removedSame as step 5
7Done10 <-> 20 <-> 30No changeFinal list: 10 ⇄ 20 ⇄ 30 head points to 10, tail points to 30
πŸ’‘ Tail updated to previous node, last node removed, list updated
Variable Tracker
VariableStartAfter Step 4After Step 5Final
headNode(10)Node(10)Node(10)Node(10)
tailNode(40)Node(30)Node(30)Node(30)
tail.nextnullNode(40)nullnull
List Size4 nodes4 nodes3 nodes3 nodes
Key Moments - 3 Insights
Why do we check if head === tail before deleting?
Because if head equals tail, the list has only one node. Deleting it means the list becomes empty, so both head and tail must be set to null (see step 3 in execution_table).
Why do we set tail.next to null after moving tail?
To break the link to the old last node and avoid dangling references. This ensures the new tail is the end of the list (see step 5 in execution_table).
What happens if the list is empty when deleteFromEnd is called?
The function returns immediately without changes because tail is null, so no deletion occurs (see step 2 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the tail pointer after step 4?
ANode with data 40
BNode with data 30
CNode with data 20
DNull
πŸ’‘ Hint
Check the 'Pointer Changes' column at step 4 in execution_table
At which step does the list size reduce from 4 to 3 nodes?
AStep 2
BStep 3
CStep 5
DStep 7
πŸ’‘ Hint
Look at the 'Nodes in List' and 'Visual State' columns in execution_table
If the list had only one node, what would happen to head and tail after deletion?
ABoth head and tail become null
BOnly tail becomes null
COnly head becomes null
DNo change
πŸ’‘ Hint
Refer to step 3 in execution_table and key_moments about single node deletion
Concept Snapshot
Delete from End of Doubly Linked List:
- If list empty (tail null), do nothing
- If one node, set head and tail to null
- Else move tail to tail.prev
- Set tail.next to null to remove last node
- Old tail node is removed from memory
Full Transcript
This visual trace shows how to delete the last node from a doubly linked list. We start at the tail node and check if the list is empty or has only one node. If empty, no action is taken. If only one node, both head and tail pointers are set to null, making the list empty. Otherwise, the tail pointer moves to the previous node, and the new tail's next pointer is set to null to disconnect the old tail. The old tail node is then removed. The variable tracker shows how head and tail pointers change, and the list size decreases by one. Key moments clarify why these pointer updates are necessary to maintain list integrity and avoid dangling references. The visual quiz tests understanding of pointer changes and list size updates during deletion.

Practice

(1/5)
1. What happens to the tail pointer when you delete the last node from a doubly linked list?
easy
A. It moves to the next node of the deleted node.
B. It becomes null immediately without change.
C. It moves to the previous node of the deleted node.
D. It stays pointing to the deleted node.

Solution

  1. Step 1: Understand tail pointer role

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

    When the last node is deleted, tail must move to the previous node to remain correct.
  3. Final Answer:

    It moves to the previous node of the deleted node. -> Option C
  4. Quick Check:

    Tail updates to previous node [OK]
Hint: Tail always points to last node, update to previous on delete [OK]
Common Mistakes:
  • Setting tail to null without checking list emptiness
  • Not updating tail pointer after deletion
  • Moving tail to next node which doesn't exist
2. Which of the following is the correct syntax to remove the last node from a doubly linked list in JavaScript?
easy
A. tail = tail.prev; tail.next = null;
B. tail = tail.next; tail.prev = null;
C. tail = tail.prev; tail.prev = null;
D. tail = null; head = null;

Solution

  1. Step 1: Move tail pointer to previous node

    To delete last node, tail moves to tail.prev.
  2. Step 2: Set new tail's next to null

    New tail's next must be null to mark end of list.
  3. Final Answer:

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

    Correct tail update and next nullify [OK]
Hint: Tail moves back, next pointer cleared [OK]
Common Mistakes:
  • Moving tail forward instead of backward
  • Not setting tail.next to null
  • Setting tail.prev to null incorrectly
3. Consider the following doubly linked list: 1 <-> 2 <-> 3 <-> 4. After deleting the last node, what will be the new list?
medium
A. 2 <-> 3 <-> 4
B. 1 <-> 2 <-> 3
C. 1 <-> 2 <-> 3 <-> 4
D. 1 <-> 2

Solution

  1. Step 1: Identify last node

    Last node is 4, which will be removed.
  2. Step 2: Remove last node and update tail

    Tail moves to node 3, and node 3's next becomes null.
  3. Final Answer:

    1 <-> 2 <-> 3 -> Option B
  4. Quick Check:

    Last node removed, list ends at 3 [OK]
Hint: Remove last node, tail moves back [OK]
Common Mistakes:
  • Forgetting to update tail pointer
  • Not setting new tail's next to null
  • Removing wrong node
4. Given this code snippet to delete the last node from a doubly linked list, what is the error?
if (tail === null) return;
tail = tail.prev;
tail.next = null;
medium
A. It should check if tail.next is null before deleting.
B. It incorrectly sets tail.next to null before moving tail.
C. It should set tail.prev to null instead of tail.next.
D. It does not handle the case when list has only one node.

Solution

  1. Step 1: Check single node case

    If list has one node, tail.prev is null, so tail becomes null.
  2. Step 2: Problem with tail.next assignment

    After tail becomes null, tail.next causes error because tail is null.
  3. Final Answer:

    It does not handle the case when list has only one node. -> Option D
  4. Quick Check:

    Single node deletion needs special handling [OK]
Hint: Check single node before updating tail [OK]
Common Mistakes:
  • Not checking if tail is null after update
  • Accessing properties of null tail
  • Ignoring empty list case
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30. After deleting the last node twice, what will be the state of the list?
hard
A. 10
B. 10 <-> 20
C. 20
D. Empty list

Solution

  1. Step 1: Delete last node once

    Last node 30 is removed, list becomes 10 <-> 20.
  2. Step 2: Delete last node again

    Now last node 20 is removed, list becomes 10 only.
  3. Step 3: Verify final list

    After two deletions, list is just node 10.
  4. Final Answer:

    10 -> Option A
  5. Quick Check:

    Two deletions remove last two nodes [OK]
Hint: Delete last node repeatedly removes tail nodes [OK]
Common Mistakes:
  • Assuming list becomes empty after two deletions
  • Not updating tail pointer correctly
  • Confusing node values after deletion
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