Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

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

Practice

(1/5)
1. What does the operation insert at beginning do in a linked list?
easy
A. Deletes the first node of the list
B. Adds a new node at the start of the list
C. Adds a new node at the end of the list
D. Deletes the last node of the list

Solution

  1. Step 1: Understand the meaning of 'insert at beginning'

    Inserting at the beginning means adding a new element before the current first element.
  2. Step 2: Identify the effect on the linked list

    The new node becomes the new head, pushing the old head to second position.
  3. Final Answer:

    Adds a new node at the start of the list -> Option B
  4. Quick Check:

    Insert at beginning = Add new head [OK]
Hint: Insert at beginning means new head node [OK]
Common Mistakes:
  • Confusing insert at beginning with insert at end
  • Thinking it deletes nodes instead of adding
  • Assuming it changes the last node
2. Which of the following is the correct syntax to insert a new node at the beginning of a singly linked list in Python?
easy
A. new_node = head.next; head = new_node
B. head.next = new_node; new_node = head
C. head = head.next; new_node = head
D. new_node.next = head; head = new_node

Solution

  1. Step 1: Understand pointer assignment for head insert

    To insert at beginning, new_node's next points to current head.
  2. Step 2: Update head to new_node

    Then head is updated to new_node to make it the first node.
  3. Final Answer:

    new_node.next = head; head = new_node -> Option D
  4. Quick Check:

    Set new_node.next to head, then head = new_node [OK]
Hint: Link new node to head, then update head [OK]
Common Mistakes:
  • Assigning head.next instead of new_node.next
  • Not updating head to new_node
  • Swapping assignment order incorrectly
3. What will be the linked list after executing the following code?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(10)
new_node = Node(20)
new_node.next = head
head = new_node
medium
A. 20 -> 10 -> null
B. 10 -> 20 -> null
C. 20 -> null
D. 10 -> null

Solution

  1. Step 1: Create initial list with head node 10

    Initially, head points to node with data 10 and next is None.
  2. Step 2: Insert new_node with data 20 at beginning

    new_node.next points to head (node 10), then head updated to new_node.
  3. Final Answer:

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

    New node 20 becomes head, points to 10 [OK]
Hint: New node points to old head, then becomes head [OK]
Common Mistakes:
  • Thinking new_node is added at end
  • Not updating head to new_node
  • Confusing node data order
4. Identify the error in this code snippet that tries to insert a node at the beginning of a linked list:
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(5)
new_node = Node(15)
head = new_node
new_node.next = head
medium
A. The order of assignments is wrong; new_node.next should be set before head update
B. Node class is missing a data attribute
C. head should be None initially
D. new_node should not be created before head

Solution

  1. Step 1: Analyze assignment order

    head is set to new_node first, then new_node.next is set to head (which is now new_node itself).
  2. Step 2: Identify logical error

    This causes new_node.next to point to itself, losing original list starting at old head.
  3. Final Answer:

    The order of assignments is wrong; new_node.next should be set before head update -> Option A
  4. Quick Check:

    Set new_node.next before head = new_node [OK]
Hint: Set new_node.next before updating head [OK]
Common Mistakes:
  • Updating head before linking new_node.next
  • Confusing attribute initialization
  • Assuming head must be None initially
5. Given a linked list with nodes containing values [30, 20, 10] (30 is head), which sequence of operations correctly inserts a new node with value 40 at the beginning and results in the list 40 -> 30 -> 20 -> 10 -> null?
hard
A. new_node = Node(40); new_node.next = None; head = new_node
B. new_node = Node(40); head.next = new_node; head = new_node
C. new_node = Node(40); new_node.next = head; head = new_node
D. new_node = Node(40); head = new_node; new_node.next = head

Solution

  1. Step 1: Create new node with value 40

    new_node is created with data 40 and next initially None.
  2. Step 2: Link new_node to current head

    Set new_node.next to current head (node 30) to keep existing list after new node.
  3. Step 3: Update head to new_node

    Head pointer is updated to new_node, making it the first node.
  4. Final Answer:

    new_node = Node(40); new_node.next = head; head = new_node -> Option C
  5. Quick Check:

    Link new node to old head, then update head [OK]
Hint: Link new node to head, then update head pointer [OK]
Common Mistakes:
  • Linking head.next instead of new_node.next
  • Updating head before linking new_node.next
  • Setting new_node.next to None losing list
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