Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Delete Node at End in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
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 the last node from a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    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

    def delete_end(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        second_last = self.head
        while second_last.next.next:
            second_last = second_last.next
        second_last.next = None

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=' -> ' if current.next else ' -> null\n')
            current = current.next

ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.delete_end()
ll.print_list()
A10 -> 20 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Deleting the last node removes the node with value 30.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node from a single-node linked list
What is the printed state of the 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.next = None

class LinkedList:
    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

    def delete_end(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        second_last = self.head
        while second_last.next.next:
            second_last = second_last.next
        second_last.next = None

    def print_list(self):
        if not self.head:
            print('null')
            return
        current = self.head
        while current:
            print(current.data, end=' -> ' if current.next else ' -> null\n')
            current = current.next

ll = LinkedList()
ll.append(100)
ll.delete_end()
ll.print_list()
AError: Cannot delete from empty list
B100 -> null
C100 -> 100 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the only node results in an empty list.
🔧 Debug
advanced
2:00remaining
Identify the error in deleting the last node
What error does this code raise when trying to delete the last node from a linked list with two nodes?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    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

    def delete_end(self):
        if not self.head:
            return
        current = self.head
        while current:
            current = current.next
        current.next = None

ll = LinkedList()
ll.append(1)
ll.append(2)
ll.delete_end()
AAttributeError: 'NoneType' object has no attribute 'next'
BNo error, list becomes 1 -> null
CTypeError: 'int' object is not callable
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check how the loop moves to the last node and what happens next.
🧠 Conceptual
advanced
1:30remaining
Why do we need to traverse to the second last node to delete the last node?
In a singly linked list, why must we stop at the second last node to delete the last node?
ABecause the head node always points to the second last node
BBecause the second last node's next pointer needs to be set to None to remove the last node
CBecause the last node cannot be accessed directly without the second last node's data
DBecause the last node's next pointer points to the second last node
Attempts:
2 left
💡 Hint
Think about how nodes are connected in a singly linked list.
❓ Predict Output
expert
2:30remaining
Output after multiple deletions at the end
What is the printed state of the linked list after deleting the last node three times from the initial list 5 -> 10 -> 15 -> 20 -> 25 -> null?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    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

    def delete_end(self):
        if not self.head:
            return
        if not self.head.next:
            self.head = None
            return
        second_last = self.head
        while second_last.next.next:
            second_last = second_last.next
        second_last.next = None

    def print_list(self):
        current = self.head
        while current:
            print(current.data, end=' -> ' if current.next else ' -> null\n')
            current = current.next

ll = LinkedList()
for value in [5, 10, 15, 20, 25]:
    ll.append(value)

ll.delete_end()
ll.delete_end()
ll.delete_end()
ll.print_list()
A5 -> 10 -> 15 -> null
B5 -> null
C5 -> 10 -> null
D5 -> 10 -> 15 -> 20 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions, three nodes are removed.

Practice

(1/5)
1. What is the main purpose of the Delete Node at End operation in a singly linked list?
easy
A. Remove the first node from the linked list
B. Add a new node at the end of the linked list
C. Remove the last node from the linked list
D. Reverse the linked list

Solution

  1. Step 1: Understand the operation

    The operation aims to remove a node from the end of the singly linked list.
  2. Step 2: Identify the correct description

    Removing the last node means deleting the tail node, not adding or reversing nodes.
  3. Final Answer:

    Remove the last node from the linked list -> Option C
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Deleting at end means removing the last node [OK]
Common Mistakes:
  • Confusing delete at end with delete at beginning
  • Thinking it adds a node instead of removing
  • Mixing up with reversing the list
2. Which of the following is the correct syntax to delete the last node in a singly linked list in Python?
easy
A. Set last node's next to head
B. Set head = null directly
C. Create a new node and assign it to head
D. Traverse to second last node and set its next to null

Solution

  1. Step 1: Understand deletion at end

    To delete the last node, we must find the second last node and make its next pointer null.
  2. Step 2: Check each option

    Setting head = null deletes the whole list, not just last node. Setting last node's next to head creates a cycle. Creating a new node and assigning to head adds a node, not deletes.
  3. Final Answer:

    Traverse to second last node and set its next to null -> Option D
  4. Quick Check:

    Delete last node = second last node next = null [OK]
Hint: Set second last node's next to null to delete last node [OK]
Common Mistakes:
  • Setting head to null deletes entire list
  • Creating cycles by wrong pointer assignments
  • Adding nodes instead of deleting
3. What will be the output linked list after deleting the last node from the list: 1 -> 2 -> 3 -> 4 -> null?
medium
A. 1 -> 2 -> 3 -> null
B. 1 -> 2 -> 3 -> 4 -> null
C. 2 -> 3 -> 4 -> null
D. 1 -> 2 -> null

Solution

  1. Step 1: Identify the last node

    The last node in the list is the node with value 4.
  2. Step 2: Delete the last node

    Removing node 4 means the new last node is 3, so the list ends at 3 with next as null.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option A
  4. Quick Check:

    Last node 4 removed = 1 -> 2 -> 3 [OK]
Hint: Last node removed means list ends one node earlier [OK]
Common Mistakes:
  • Not updating second last node's next to null
  • Removing wrong node (like first node)
  • Confusing output with original list
4. Consider this code snippet to delete the last node of a singly linked list:
def delete_end(head):
    if head is None:
        return None
    if head.next is None:
        return None
    temp = head
    while temp.next is not None:
        temp = temp.next
    temp.next = None
    return head
What is the error in this code?
medium
A. The loop never stops because condition is wrong
B. temp.next is null check misses second last node
C. It deletes the first node instead of last
D. No error, code works correctly

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while temp.next is not null, so temp moves to the last node.
  2. Step 2: Identify pointer update mistake

    After loop, temp points to last node, but to delete last node, we need second last node to set its next to null. Here, temp.next = null sets last node's next to null, which is already null, so no deletion happens.
  3. Final Answer:

    temp.next is null check misses second last node -> Option B
  4. Quick Check:

    Must stop at second last node to delete last [OK]
Hint: Stop at second last node, not last, to delete end node [OK]
Common Mistakes:
  • Stopping loop at last node instead of second last
  • Not handling single node list correctly
  • Setting wrong pointers causing no deletion
5. Given a singly linked list with nodes 10 -> 20 -> 30 -> 40 -> 50 -> null, write the steps to delete the last node and then add a new node with value 60 at the end. What will be the final list?
hard
A. 10 -> 20 -> 30 -> 40 -> 60 -> null
B. 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> null
C. 10 -> 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 30 -> 40 -> 50 -> null

Solution

  1. Step 1: Delete the last node (50)

    Traverse to second last node (40) and set its next to null, removing 50.
  2. Step 2: Add new node with value 60 at end

    Traverse to last node (40) and set its next to new node with value 60, new node's next is null.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> 60 -> null -> Option A
  4. Quick Check:

    Delete last 50, add 60 at end = 10->20->30->40->60 [OK]
Hint: Delete last node, then link new node at end [OK]
Common Mistakes:
  • Not updating second last node's next to null before adding
  • Appending new node without deleting last node
  • Confusing node values or order
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