Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Delete from End of Doubly Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting the last node?
Consider a doubly linked list with nodes 1 <-> 2 <-> 3 <-> null. After deleting the last node, what is the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

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

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

  deleteFromEnd() {
    if (!this.head) return;
    if (!this.head.next) {
      this.head = null;
      return;
    }
    let curr = this.head;
    while (curr.next) {
      curr = curr.next;
    }
    curr.prev.next = null;
  }

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

const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.deleteFromEnd();
dll.printList();
A2 -> 3 -> null
B1 -> 2 -> 3 -> null
C1 -> 2 -> null
D1 -> null
Attempts:
2 left
💡 Hint
Deleting from end removes the last node and updates the previous node's next pointer.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting from a single-node list?
Given a doubly linked list with a single node 5, what is the printed list after deleting from the end?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

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

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

  deleteFromEnd() {
    if (!this.head) return;
    if (!this.head.next) {
      this.head = null;
      return;
    }
    let curr = this.head;
    while (curr.next) {
      curr = curr.next;
    }
    curr.prev.next = null;
  }

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

const dll = new DoublyLinkedList();
dll.append(5);
dll.deleteFromEnd();
dll.printList();
A5 -> null
B5
CError: Cannot read property 'next' of null
Dnull
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2:00remaining
Why does this deleteFromEnd method cause an error?
This deleteFromEnd method sometimes throws an error. Why? ```js deleteFromEnd() { if (!this.head) return; let curr = this.head; while (curr.next) { curr = curr.next; } curr.prev.next = null; } ```
AIt fails because head is not updated after deletion.
BIt fails when the list has only one node because curr.prev is null, so curr.prev.next causes an error.
CIt fails because curr.next is set to null before accessing curr.prev.
DIt fails because curr.next is never null, causing an infinite loop.
Attempts:
2 left
💡 Hint
Think about what happens when the list has only one node.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple deletions from end?
Starting with list 10 <-> 20 <-> 30 <-> 40 <-> null, after calling deleteFromEnd() twice, what is the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

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

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

  deleteFromEnd() {
    if (!this.head) return;
    if (!this.head.next) {
      this.head = null;
      return;
    }
    let curr = this.head;
    while (curr.next) {
      curr = curr.next;
    }
    curr.prev.next = null;
  }

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

const dll = new DoublyLinkedList();
dll.append(10);
dll.append(20);
dll.append(30);
dll.append(40);
dll.deleteFromEnd();
dll.deleteFromEnd();
dll.printList();
A10 -> 20 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Each deleteFromEnd removes the last node.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of deleting from the end in a doubly linked list without tail pointer?
If a doubly linked list does NOT maintain a tail pointer, what is the time complexity of deleting the last node?
AO(n), because you must traverse from head to find the last node.
BO(log n), because the list is balanced.
CO(1), because you can directly access the last node via prev pointer.
DO(n^2), because each deletion requires traversing the entire list multiple times.
Attempts:
2 left
💡 Hint
Without a tail pointer, you start at head and move forward to find the last 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