0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delete Node at Beginning 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 first node?
Consider a singly linked list with nodes 10 -> 20 -> 30 -> null. After deleting the node at the beginning, what is the printed state of the list?
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_at_beginning(self):
        if self.head:
            self.head = self.head.next

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

ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.delete_at_beginning()
ll.print_list()
Anull
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D30 -> null
Attempts:
2 left
💡 Hint
Deleting at beginning removes the first node and moves head to next node.
Predict Output
intermediate
2:00remaining
What happens when deleting from a single-node list?
Given a linked list with a single node 5 -> null, what is the printed output after deleting the node at the beginning?
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_at_beginning(self):
        if self.head:
            self.head = self.head.next

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

ll = LinkedList()
ll.append(5)
ll.delete_at_beginning()
ll.print_list()
A5 -> null
Bnull
CError
D5
Attempts:
2 left
💡 Hint
Deleting the only node leaves the list empty.
🔧 Debug
advanced
2:00remaining
Identify the error in this delete at beginning method
What error will this code produce when called on an empty list?
DSA Python
def delete_at_beginning(self):
    if self.head.next:
        self.head = self.head.next
    else:
        self.head = None
AAttributeError when list is empty
BNo error, works correctly
CTypeError when list is empty
DIndexError when list has multiple nodes
Attempts:
2 left
💡 Hint
Check what happens if head is None before accessing head.next.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of deleting the first node in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of deleting the node at the beginning?
AO(1)
BO(n)
CO(log n)
DO(n^2)
Attempts:
2 left
💡 Hint
Deleting the first node only changes the head pointer.
🚀 Application
expert
3:00remaining
After multiple deletions at beginning, what is the list state?
Starting with the list 1 -> 2 -> 3 -> 4 -> 5 -> null, after deleting the first node three times, what is the printed list?
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_at_beginning(self):
        if self.head:
            self.head = self.head.next

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

ll = LinkedList()
for i in range(1, 6):
    ll.append(i)
ll.delete_at_beginning()
ll.delete_at_beginning()
ll.delete_at_beginning()
ll.print_list()
A2 -> 3 -> 4 -> 5 -> null
B3 -> 4 -> 5 -> null
Cnull
D4 -> 5 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.