0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 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.