0
0
DSA Pythonprogramming~20 mins

Pop Using Linked List Node in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked List Pop 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 popping the last node from the linked list?
Given the linked list and the pop operation code below, what is the printed linked list after popping 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 pop(self):
        if not self.head:
            return None
        if not self.head.next:
            popped = self.head.data
            self.head = None
            return popped
        current = self.head
        while current.next.next:
            current = current.next
        popped = current.next.data
        current.next = None
        return popped

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

ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.pop()
ll.print_list()
A10 -> 20 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Think about what happens to the last node when you pop it from the list.
Predict Output
intermediate
2:00remaining
What is the output after popping from a single-node linked list?
Consider the linked list with one node and the pop operation below. What is printed after popping the only 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 pop(self):
        if not self.head:
            return None
        if not self.head.next:
            popped = self.head.data
            self.head = None
            return popped
        current = self.head
        while current.next.next:
            current = current.next
        popped = current.next.data
        current.next = None
        return popped

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

ll = LinkedList()
ll.append(100)
ll.pop()
ll.print_list()
A100
B100 -> null
CError: Cannot pop from empty list
Dnull
Attempts:
2 left
💡 Hint
What happens to the head when you pop the only node?
Predict Output
advanced
2:00remaining
What is the output after popping twice from the linked list?
Given the linked list and pop method below, what is the printed linked list after popping twice?
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 pop(self):
        if not self.head:
            return None
        if not self.head.next:
            popped = self.head.data
            self.head = None
            return popped
        current = self.head
        while current.next.next:
            current = current.next
        popped = current.next.data
        current.next = None
        return popped

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

ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.pop()
ll.pop()
ll.print_list()
A1 -> 2 -> 3 -> null
B1 -> 2 -> null
C2 -> 3 -> 4 -> null
D1 -> 3 -> null
Attempts:
2 left
💡 Hint
Each pop removes the last node. Think about which nodes remain after two pops.
🔧 Debug
advanced
2:00remaining
Why does this pop method cause an error on a single-node list?
Examine the pop method below. It causes an error when popping from a list with only one node. What is the cause?
DSA Python
def pop(self):
    if not self.head:
        return None
    current = self.head
    while current.next.next:
        current = current.next
    popped = current.next.data
    current.next = None
    return popped
AIt causes infinite loop when the list has two nodes.
BIt returns None instead of the popped value when the list has two nodes.
CIt does not handle the case when the list has only one node, causing AttributeError.
DIt does not handle the case when the list has exactly two nodes, causing AttributeError.
Attempts:
2 left
💡 Hint
Check what happens when current.next.next is accessed on a single-node list.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of popping the last node in a singly linked list without tail pointer?
Consider a singly linked list with no tail pointer. What is the time complexity of the pop operation that removes the last node?
AO(n) because we must traverse the list to find the second last node
BO(1) because we can directly access the last node
CO(log n) because of binary search on nodes
DO(n^2) because of nested traversal
Attempts:
2 left
💡 Hint
Think about how you find the node before the last in a singly linked list.