Bird
Raised Fist0
DSA Javascriptprogramming

Delete from End of Doubly Linked List in DSA Javascript

Choose your learning style10 modes available
Mental Model
To remove the last item, find the tail and unlink it from the list so the second last becomes the new tail.
Analogy: Imagine a train of connected carriages. To remove the last carriage, you detach it from the one before it, making the second last carriage the new end.
head -> 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 ← tail
null ← 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 -> null
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5, delete from end
Goal: Remove the last node (5) and update the list tail to node 4
Step 1: start at head (node 1), move forward to find tail
1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 ← tail
null ← 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 -> null ↑
Why: We need to find the last node to remove it
Step 2: tail is node 5, set tail to node 4 and remove node 5
1 ↔ 2 ↔ 3 ↔ 4 ← tail
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
Why: Removing last node means tail moves to previous node and last node is unlinked
Step 3: set node 4's next pointer to null to finalize removal
1 ↔ 2 ↔ 3 ↔ 4 ← tail
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
Why: Breaking link to removed node ensures list ends properly
Result:
1 ↔ 2 ↔ 3 ↔ 4 ← tail
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
Annotated Code
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
      return;
    }
    this.tail.next = newNode;
    newNode.prev = this.tail;
    this.tail = newNode;
  }

  deleteFromEnd() {
    if (!this.tail) return; // empty list
    if (this.head === this.tail) {
      this.head = null;
      this.tail = null;
      return;
    }
    this.tail = this.tail.prev; // move tail back
    this.tail.next = null; // unlink old tail
  }

  printList() {
    let curr = this.head;
    let result = '';
    while (curr) {
      result += curr.data;
      if (curr.next) result += ' ↔ ';
      curr = curr.next;
    }
    console.log(result);
  }
}

// Driver code
const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
dll.printList();
dll.deleteFromEnd();
dll.printList();
if (!this.tail) return; // empty list
handle empty list by returning early
if (this.head === this.tail) { this.head = null; this.tail = null; return; }
handle single node list by clearing head and tail
this.tail = this.tail.prev; // move tail back
move tail pointer to previous node to remove last node
this.tail.next = null; // unlink old tail
unlink old tail by setting next of new tail to null
OutputSuccess
1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 1 ↔ 2 ↔ 3 ↔ 4
Complexity Analysis
Time: O(1) because we directly access the tail and update pointers without traversal
Space: O(1) because no extra space is used beyond variables
vs Alternative: Compared to singly linked list where deleting end requires traversal O(n), doubly linked list allows O(1) deletion from end
Edge Cases
empty list
deleteFromEnd returns immediately without error
DSA Javascript
if (!this.tail) return; // empty list
single node list
head and tail become null, list becomes empty
DSA Javascript
if (this.head === this.tail) {
      this.head = null;
      this.tail = null;
      return;
    }
When to Use This Pattern
When you need to remove the last element quickly from a list, use a doubly linked list because it keeps a tail pointer and backward links for O(1) deletion.
Common Mistakes
Mistake: Not updating the tail pointer after deletion, leaving tail pointing to removed node
Fix: Always move tail to tail.prev before unlinking the old tail
Mistake: Not handling empty or single node list cases, causing errors
Fix: Add checks for empty list and single node list before deletion
Summary
Deletes the last node from a doubly linked list by updating tail and unlinking the last node.
Use when you want efficient removal from the end of a list.
The key is to move tail back and break the link to the removed node.

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