Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Delete from End of Doubly Linked List in DSA Python - 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
Output after deleting last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last node?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_from_end(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        last = self.head
        while last.next:
            last = last.next
        last.prev.next = None

    def print_list(self):
        if not self.head:
            print("null")
            return
        current = self.head
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" -> ".join(result) + " -> null")

# Setup
dll = DoublyLinkedList()
dll.append(10)
dll.append(20)
dll.append(30)
dll.delete_from_end()
dll.print_list()
A20 -> 30 -> null
B10 -> 20 -> 30 -> null
C10 -> 20 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Think about what happens to the last node and the previous node's next pointer.
❓ Predict Output
intermediate
2:00remaining
Output after deleting last node from single-node doubly linked list
What is the printed state of the doubly linked list after deleting the last node when the list has only one node?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_from_end(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        last = self.head
        while last.next:
            last = last.next
        last.prev.next = None

    def print_list(self):
        if not self.head:
            print("null")
            return
        current = self.head
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" -> ".join(result) + " -> null")

# Setup
single_node_list = DoublyLinkedList()
single_node_list.append(100)
single_node_list.delete_from_end()
single_node_list.print_list()
Anull
B100 -> null
CError
D100
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2:00remaining
Identify the error in delete_from_end method
Which option contains the error that will cause a broken list when deleting the last node from a doubly linked list with two nodes?
DSA Python
def delete_from_end(self):
    if not self.head:
        return
    if not self.head.next:
        self.head = None
        return
    last = self.head
    while last.next:
        last = last.next
    last.prev = None  # Error here
Alast.prev = None # Incorrect, breaks list
Blast.prev.next = None # Correctly removes last node
Clast.next = None # Correctly removes last node
Dself.head = None # Incorrect, deletes entire list
Attempts:
2 left
💡 Hint
Think about which pointer needs to be updated to remove the last node properly.
🧠 Conceptual
advanced
2:00remaining
Effect of deleting last node on doubly linked list pointers
After deleting the last node from a doubly linked list, which pointers are updated to maintain list integrity?
AThe previous node's prev pointer is set to None.
BThe head pointer is set to None regardless of list size.
CThe last node's next pointer is set to the head node.
DThe previous node's next pointer is set to None; the last node is removed.
Attempts:
2 left
💡 Hint
Consider what happens to the node before the last node after deletion.
❓ Predict Output
expert
3:00remaining
Output after multiple deletions from end of doubly linked list
What is the printed state of the doubly linked list after deleting the last node three times in a row?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def delete_from_end(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        last = self.head
        while last.next:
            last = last.next
        last.prev.next = None

    def print_list(self):
        if not self.head:
            print("null")
            return
        current = self.head
        result = []
        while current:
            result.append(str(current.data))
            current = current.next
        print(" -> ".join(result) + " -> null")

# Setup
dll = DoublyLinkedList()
dll.append(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.append(5)
dll.delete_from_end()
dll.delete_from_end()
dll.delete_from_end()
dll.print_list()
A3 -> 4 -> 5 -> null
B1 -> 2 -> null
C1 -> 2 -> 3 -> null
Dnull
Attempts:
2 left
💡 Hint
Each deletion removes the last node; track the list after each removal.

Practice

(1/5)
1. What happens when you delete a node from the end of a doubly linked list that has multiple nodes?
easy
A. The last node is removed and the second last node becomes the new tail.
B. The first node is removed and the second node becomes the new head.
C. All nodes are deleted and the list becomes empty.
D. The middle node is removed and the list remains unchanged at the ends.

Solution

  1. Step 1: Identify the last node and update pointers

    In a doubly linked list, the last node is the tail node which has its next pointer as null. Deleting the last node means updating the tail pointer to the previous node and setting its next pointer to null. The second last node becomes the new tail.
  2. Final Answer:

    The last node is removed and the second last node becomes the new tail. -> Option A
  3. Quick Check:

    Delete end node = tail moves back [OK]
Hint: Last node removed, tail moves to previous node [OK]
Common Mistakes:
  • Removing the head instead of the tail
  • Not updating the tail pointer
  • Leaving the next pointer of new tail non-null
2. Which of the following is the correct syntax to delete the last node in a doubly linked list in Python?
easy
A. head = head.prev; head.next = None
B. tail = tail.next; tail.prev = None
C. head = head.next; head.prev = None
D. tail = tail.prev; tail.next = None

Solution

  1. Step 1: Move tail pointer and update next

    To delete the last node, move tail to tail.prev (second last node), then set tail.next to None to mark the end of the list.
  2. Final Answer:

    tail = tail.prev; tail.next = None -> Option D
  3. Quick Check:

    Correct tail update syntax = tail = tail.prev; tail.next = None [OK]
Hint: Tail moves back, next pointer set to None [OK]
Common Mistakes:
  • Moving tail forward instead of backward
  • Setting prev pointer instead of next
  • Changing head pointer instead of tail
3. What will be the output after deleting the last node from the doubly linked list shown below?
Initial list: 10 <-> 20 <-> 30 <-> 40 <-> None
Operation: Delete from end
What is the resulting list?
medium
A. 10 <-> 20 <-> 30 <-> 40 <-> None
B. 10 <-> 20 <-> 30 <-> None
C. 20 <-> 30 <-> 40 <-> None
D. 10 <-> 20 <-> None

Solution

  1. Step 1: Identify last node and update

    The last node is 40, which is removed by setting node 30's next pointer to None, making it the new tail. Result: 10 <-> 20 <-> 30 <-> None.
  2. Final Answer:

    10 <-> 20 <-> 30 <-> None -> Option B
  3. Quick Check:

    Last node 40 removed, tail is 30 [OK]
Hint: Remove last node, update previous node's next to None [OK]
Common Mistakes:
  • Not updating the next pointer of new tail
  • Removing the wrong node (head or middle)
  • Leaving dangling pointers causing errors
4. Consider the following code snippet to delete the last node from a doubly linked list:
if tail is None:
    print('List is empty')
else:
    tail = tail.prev
    tail.next = None
What error can occur when this code runs on a list with only one node?
medium
A. AttributeError because tail.prev is None and tail.next is accessed
B. No error, code works fine for single node list
C. SyntaxError due to missing colon
D. IndexError because tail is out of range

Solution

  1. Step 1: Trace execution for single node

    For one node, tail.prev is None, so tail = None, then tail.next = None accesses None.next causing AttributeError.
  2. Final Answer:

    AttributeError because tail.prev is None and tail.next is accessed -> Option A
  3. Quick Check:

    Single node delete needs extra check [OK]
Hint: Check if tail.prev is None before setting tail.next [OK]
Common Mistakes:
  • Not handling single node list case
  • Assuming tail.prev always exists
  • Ignoring NoneType attribute errors
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> 35 <-> 45 <-> None. You delete the last node twice in a row. What will be the final list?
hard
A. 5 <-> 15 <-> 25 <-> 35 <-> 45 <-> None
B. 5 <-> 15 <-> 25 <-> 35 <-> None
C. 5 <-> 15 <-> 25 <-> None
D. 15 <-> 25 <-> 35 <-> None

Solution

  1. Step 1: Perform two deletions

    Initial: 5 <-> 15 <-> 25 <-> 35 <-> 45. First delete removes 45 (tail=35). Second delete removes 35 (tail=25). Final: 5 <-> 15 <-> 25 <-> None.
  2. Final Answer:

    5 <-> 15 <-> 25 <-> None -> Option C
  3. Quick Check:

    Two deletes remove last two nodes [OK]
Hint: Each delete removes one tail node [OK]
Common Mistakes:
  • Deleting only one node instead of two
  • Not updating tail pointer after each delete
  • Confusing head and tail nodes
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