0
0
DSA Pythonprogramming~20 mins

Insert at Beginning Head Insert in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Head Insert 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 inserting nodes at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning one by one. What is the final state of the linked list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

    def __str__(self):
        result = []
        current = self.head
        while current:
            result.append(str(current.data))
            current = current.next
        return ' -> '.join(result) + ' -> null'

ll = LinkedList()
ll.insert_at_beginning(10)
ll.insert_at_beginning(20)
ll.insert_at_beginning(30)
print(ll)
A10 -> null
B10 -> 20 -> 30 -> null
C30 -> 20 -> 10 -> null
D30 -> null
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
Predict Output
intermediate
2:00remaining
What happens if we insert None at the beginning?
Given the linked list code for inserting at the beginning, what will be the output if we insert None as data once into an empty list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

    def __str__(self):
        result = []
        current = self.head
        while current:
            result.append(str(current.data))
            current = current.next
        return ' -> '.join(result) + ' -> null'

ll = LinkedList()
ll.insert_at_beginning(None)
print(ll)
AEmpty list
Bnull
CError: Cannot insert None
DNone -> null
Attempts:
2 left
💡 Hint
The code does not restrict data values; None is a valid data.
🔧 Debug
advanced
2:30remaining
Why does this head insert code cause an infinite loop?
This code tries to insert a node at the beginning but causes an infinite loop when printing. What is the bug?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_beginning(self, data):
        new_node = Node(data)
        self.head = new_node
        new_node.next = self.head

    def __str__(self):
        result = []
        current = self.head
        while current:
            result.append(str(current.data))
            current = current.next
        return ' -> '.join(result) + ' -> null'

ll = LinkedList()
ll.insert_at_beginning(5)
ll.insert_at_beginning(10)
print(ll)
AThe print statement is outside the class and causes an error
Bnew_node.next is set after updating head, causing new_node.next to point to itself, creating a cycle
CThe Node class is missing the next attribute initialization
DThe head is never updated, so the list remains empty
Attempts:
2 left
💡 Hint
Check the order of assignments in insert_at_beginning method.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of inserting at the beginning of a singly linked list?
Consider inserting a new node at the beginning (head) of a singly linked list. What is the time complexity of this operation?
AO(1) - Constant time
BO(n) - Linear time
CO(log n) - Logarithmic time
DO(n^2) - Quadratic time
Attempts:
2 left
💡 Hint
Think about how many nodes you need to visit to insert at the head.
Predict Output
expert
2:30remaining
What is the output after multiple head inserts and one traversal?
Given the code below, what is printed after all insertions?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

    def traverse_and_collect(self):
        result = []
        current = self.head
        while current:
            result.append(current.data)
            current = current.next
        return result

ll = LinkedList()
for value in [1, 2, 3, 4]:
    ll.insert_at_beginning(value)
print(ll.traverse_and_collect())
A[4, 3, 2, 1]
B[1, 2, 3, 4]
C[1]
D[]
Attempts:
2 left
💡 Hint
Each new value is inserted at the front, so the last inserted is first in the list.