Insert at Beginning (Head Insert) in a Linked List
📖 Scenario: Imagine you are managing a line of people waiting to enter a concert. Sometimes, a VIP arrives and needs to be placed right at the front of the line. We will simulate this using a linked list where each person is a node.
🎯 Goal: You will build a simple linked list and learn how to insert a new node at the beginning (head) of the list. This means the new person will be first in line.
📋 What You'll Learn
Create a Node class with data and next attributes
Create a LinkedList class with a head attribute
Write a method insert_at_beginning to add a new node at the start
Print the linked list after insertion to show the order
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like music playlists, undo functionality in apps, and managing queues where order matters.
💼 Career
Understanding linked lists and how to insert nodes is fundamental for software developers, especially when working with dynamic data structures and memory management.
Progress0 / 4 steps
1
Create Node and LinkedList classes
Create a class called Node with an __init__ method that takes data and sets self.data = data and self.next = None. Then create a class called LinkedList with an __init__ method that sets self.head = None.
DSA Python
Hint
Think of Node as a person in line holding data and a pointer to the next person. The LinkedList keeps track of the first person with head.
2
Add insert_at_beginning method
Inside the LinkedList class, define a method called insert_at_beginning that takes self and data. Create a new Node with data. Set the new node's next to the current head. Then update head to the new node.
DSA Python
Hint
Remember, the new node points to the old first node, then becomes the new first node.
3
Add method to print the linked list
Inside the LinkedList class, define a method called print_list that takes self. Use a variable current starting at head. Use a while loop to print current.data followed by ' -> ' and move current to current.next. After the loop, print None.
DSA Python
Hint
Traverse the list from head to end, printing each node's data followed by an arrow.
4
Insert nodes and print the list
Create a LinkedList object called ll. Use ll.insert_at_beginning(30), then ll.insert_at_beginning(20), then ll.insert_at_beginning(10). Finally, call ll.print_list() to display the list.
DSA Python
Hint
Insert 30 first, then 20 before it, then 10 before 20. The list should print from 10 to 30.
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
Step 1: Understand the meaning of 'insert at beginning'
Inserting at the beginning means adding a new element before the current first element.
Step 2: Identify the effect on the linked list
The new node becomes the new head, pushing the old head to second position.
Final Answer:
Adds a new node at the start of the list -> Option B
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
Step 1: Understand pointer assignment for head insert
To insert at beginning, new_node's next points to current head.
Step 2: Update head to new_node
Then head is updated to new_node to make it the first node.
Final Answer:
new_node.next = head; head = new_node -> Option D
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
Step 1: Create initial list with head node 10
Initially, head points to node with data 10 and next is None.
Step 2: Insert new_node with data 20 at beginning
new_node.next points to head (node 10), then head updated to new_node.
Final Answer:
20 -> 10 -> null -> Option A
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
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).
Step 2: Identify logical error
This causes new_node.next to point to itself, losing original list starting at old head.
Final Answer:
The order of assignments is wrong; new_node.next should be set before head update -> Option A
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
Step 1: Create new node with value 40
new_node is created with data 40 and next initially None.
Step 2: Link new_node to current head
Set new_node.next to current head (node 30) to keep existing list after new node.
Step 3: Update head to new_node
Head pointer is updated to new_node, making it the first node.
Final Answer:
new_node = Node(40); new_node.next = head; head = new_node -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.