📖 Scenario: You are building a simple linked list to manage a list of tasks. You want to add new tasks at the beginning of the list so that the most recent task is always at the front.
🎯 Goal: Create a singly linked list and implement the insertion of new nodes at the beginning (head) of the list. Then print the list to see the order of tasks.
📋 What You'll Learn
Define a Node struct with int data and Node* next
Create a head pointer initialized to nullptr
Write a function insertAtBeginning that takes an int and inserts a new node at the head
Write a function printList that prints the list nodes in order separated by -> and ends with null
💡 Why This Matters
🌍 Real World
Linked lists are used in many software systems to manage dynamic collections where items are frequently added or removed, such as task schedulers or undo history.
💼 Career
Understanding linked lists and how to insert nodes at the beginning is a fundamental skill for software developers, especially for roles involving low-level programming, system design, or interview preparation.
Progress0 / 4 steps
1
Create the Node struct and initialize head
Define a struct called Node with an int data and a Node* next. Then create a Node* pointer called head and set it to nullptr.
DSA C++
Hint
Think of Node as a box holding a number and a pointer to the next box. head points to the first box or is nullptr if empty.
2
Create insertAtBeginning function
Write a function called insertAtBeginning that takes an int value. Inside, create a new Node with data set to value, next set to head, then update head to this new node.
DSA C++
Hint
Make a new box, put the value inside, point it to the current first box, then make it the new first box.
3
Create printList function
Write a function called printList that uses a Node* variable called current starting at head. Use a while loop to print each node's data followed by -> until current is nullptr. After the loop, print null.
DSA C++
Hint
Walk through each box from the first, print its number and arrow, stop when no more boxes, then print null.
4
Insert nodes and print the list
Call insertAtBeginning three times with values 10, 20, and 30 in that order. Then call printList() to display the list.
DSA C++
Hint
Remember the newest value goes to the front, so the list prints from last inserted to first.
Practice
(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Adds a new node at the end of the list
B. Deletes the first node of the list
C. Adds a new node at the start of the list, making it the new head
D. Searches for a node at the beginning
Solution
Step 1: Understand the operation
Inserting at the beginning means placing a new node before the current head.
Step 2: Effect on the list
The new node becomes the head, and its next pointer points to the old head.
Final Answer:
Adds a new node at the start of the list, making it the new head -> Option C
Quick Check:
Insert at beginning = new head [OK]
Hint: New node always becomes the head in beginning insert [OK]
Common Mistakes:
Confusing insert at beginning with insert at end
Thinking it deletes nodes
Assuming it searches instead of inserts
2. Which of the following is the correct syntax to insert a new node at the beginning of a singly linked list in C++?
easy
A. head->next = newNode; newNode = head;
B. newNode->next = head; head = newNode;
C. head = newNode; newNode->next = head;
D. newNode = head; head = newNode->next;
Solution
Step 1: Link new node to current head
Set newNode->next to point to the current head node.
Step 2: Update head to new node
Assign head to newNode so it becomes the new first node.
Final Answer:
newNode->next = head; head = newNode; -> Option B
Quick Check:
Link then update head [OK]
Hint: Set newNode->next first, then update head [OK]
Common Mistakes:
Updating head before linking newNode->next
Swapping pointers incorrectly
Assigning newNode to head->next
3. What will be the output linked list after executing this code?
struct Node { int data; Node* next; };
Node* head = nullptr;
Node* newNode = new Node{10, nullptr};
newNode->next = head;
head = newNode;
newNode = new Node{20, nullptr};
newNode->next = head;
head = newNode;
Print the list from head to end.
medium
A. 20 -> 10 -> null
B. 10 -> 20 -> null
C. null -> 10 -> 20
D. 10 -> null
Solution
Step 1: Insert first node with data 10
head points to node with 10, next is nullptr.
Step 2: Insert second node with data 20 at beginning
newNode->next = head (node 10), head = newNode (node 20).
Final Answer:
20 -> 10 -> null -> Option A
Quick Check:
Last inserted node is head [OK]
Hint: Last inserted node is always head [OK]
Common Mistakes:
Assuming order is insertion order instead of reversed
Confusing next pointers
Ignoring nullptr termination
4. Identify the error in this code snippet for inserting at the beginning of a linked list:
Node* newNode = new Node();
newNode->data = 5;
head = newNode;
newNode->next = head;
medium
A. No error, code is correct
B. newNode->data is not initialized
C. head should be set to nullptr first
D. newNode->next should be assigned before head is updated
Solution
Step 1: Check pointer assignments order
Assigning head = newNode before setting newNode->next causes newNode->next to point to itself.
Step 2: Correct order
newNode->next should be set to current head before updating head to newNode.
Final Answer:
newNode->next should be assigned before head is updated -> Option D
Quick Check:
Link newNode->next before head update [OK]
Hint: Always link newNode->next before changing head [OK]
Common Mistakes:
Updating head before linking newNode->next
Forgetting to initialize newNode->data
Assuming no error in pointer assignments
5. Given a linked list with nodes 30 -> 20 -> 10 -> null, what will be the list after inserting a new node with data 40 at the beginning twice in a row?
hard
A. 40 -> 40 -> 30 -> 20 -> 10 -> null
B. 30 -> 20 -> 10 -> 40 -> 40 -> null
C. 40 -> 30 -> 20 -> 10 -> null
D. 40 -> 30 -> 20 -> 10 -> 40 -> null
Solution
Step 1: Insert first 40 at beginning
New head is 40, next points to old head 30.
Step 2: Insert second 40 at beginning
New head is another 40, next points to previous 40 node.
Final Answer:
40 -> 40 -> 30 -> 20 -> 10 -> null -> Option A
Quick Check:
Each insert adds new head [OK]
Hint: Each insert adds a new head node [OK]
Common Mistakes:
Assuming duplicate nodes merge
Inserting at end instead of beginning
Losing track of next pointers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.