Insert at Middle Specific Position in a Linked List
📖 Scenario: You are managing a list of tasks in a to-do app. Sometimes, you need to add a new task right in the middle of the list at a specific position.
🎯 Goal: Build a program that creates a linked list of tasks, then inserts a new task at a given middle position, and finally prints the updated list.
📋 What You'll Learn
Create a singly linked list with 3 tasks: 'Task1', 'Task2', 'Task3'
Add an integer variable position set to 2 to specify the insertion point
Write a function to insert a new task 'NewTask' at the position in the linked list
Print the linked list after insertion showing all tasks in order
💡 Why This Matters
🌍 Real World
Linked lists are used in many apps to manage ordered data like tasks, playlists, or messages where items can be added or removed dynamically.
💼 Career
Understanding linked list insertion is fundamental for software developers working with low-level data structures, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the initial linked list with 3 tasks
Create a singly linked list with nodes containing char task[20] and struct Node *next. Initialize the list with these exact tasks in order: "Task1", "Task2", "Task3". Use a pointer head to the first node.
DSA C
Hint
Define a struct with a char task[20] and a Node *next. Allocate memory for each node and set the tasks exactly as given.
2
Add a variable for the insertion position
Add an integer variable called position and set it to 2 to specify where the new task will be inserted.
DSA C
Hint
Just add int position = 2; inside main().
3
Insert a new task at the given position
Write code to insert a new node with task "NewTask" at the position in the linked list. Use a pointer current to traverse the list until the node before the insertion point, then link the new node properly.
DSA C
Hint
Traverse the list to the node before position, then insert the new node by adjusting pointers.
4
Print the updated linked list
Use a pointer temp to traverse the linked list from head and print each task on its own line until the end of the list.
DSA C
Hint
Use a while loop to print each task until temp is NULL.
Practice
(1/5)
1. What does inserting a node at a specific middle position in a linked list mean?
easy
A. Adding a new node only at the start of the list
B. Adding a new node exactly at the given position counting from the head
C. Adding a new node only at the end of the list
D. Replacing the node at the given position with a new node
Solution
Step 1: Understand the position concept
Inserting at a specific position means counting nodes from the head and placing the new node exactly there.
Step 2: Differentiate from other insertions
Insertion at start or end is special cases; here we insert in the middle without removing nodes.
Final Answer:
Adding a new node exactly at the given position counting from the head -> Option B
Quick Check:
Insert at position = Insert node at that place [OK]
Hint: Insert at position means count nodes and place new node there [OK]
Common Mistakes:
Confusing insertion with replacement
Thinking insertion only happens at start or end
Ignoring the position count from head
2. Which of the following C code snippets correctly inserts a new node at position 3 in a singly linked list?
easy
A. Traverse to node 2, set newNode->next = current->next; current->next = newNode;
B. Set newNode->next = head; head = newNode;
C. Traverse to node 3, set current->next = newNode; newNode->next = NULL;
D. Set newNode->next = NULL; current->next = newNode;
Solution
Step 1: Identify correct traversal
To insert at position 3, we must reach node 2 (one before position 3).
Step 2: Link new node correctly
Set newNode->next to current->next, then current->next to newNode to insert properly.
Final Answer:
Traverse to node 2, set newNode->next = current->next; current->next = newNode; -> Option A
Quick Check:
Insert at pos 3 = link after node 2 [OK]
Hint: Insert after (position-1)th node by adjusting pointers [OK]
Common Mistakes:
Inserting at wrong node position
Not updating newNode->next before linking
Setting newNode->next to NULL incorrectly
3. What will be the linked list after inserting a node with value 99 at position 3 in the list 10 -> 20 -> 30 -> 40 -> NULL?
medium
A. 99 -> 10 -> 20 -> 30 -> 40 -> NULL
B. 10 -> 20 -> 30 -> 99 -> 40 -> NULL
C. 10 -> 20 -> 30 -> 40 -> 99 -> NULL
D. 10 -> 20 -> 99 -> 30 -> 40 -> NULL
Solution
Step 1: Identify position 3 in the list
Positions: 1=10, 2=20, 3=30, so new node goes before 30.
Step 2: Insert new node at position 3
New node with 99 points to 30, node 20 points to new node.
Final Answer:
10 -> 20 -> 99 -> 30 -> 40 -> NULL -> Option D
Quick Check:
Insert at pos 3 places 99 before 30 [OK]
Hint: Insert at pos 3 means new node is third in list [OK]
Common Mistakes:
Inserting after instead of at position
Appending at end instead of middle
Confusing node values and positions
4. Given the following code snippet to insert at position 2, what is the error?
C. Loop runs one time too many causing temp to be NULL
D. Head pointer is not updated after insertion
Solution
Step 1: Analyze loop for position 2
Loop should run from i=1 to i < pos-1 to stop at node before insertion point.
Step 2: Check temp usage after loop
Original loop runs one time too many, moving temp to node 2 instead of node 1, causing incorrect insertion or NULL access.
Final Answer:
Loop runs one time too many causing temp to be NULL -> Option C
Quick Check:
Loop overshoots node before position [OK]
Hint: Loop should stop at position-1 to avoid NULL [OK]
Common Mistakes:
Loop runs to pos instead of pos-1
Not checking for NULL before accessing temp->next
Forgetting to allocate memory for new node
5. You have a singly linked list with 5 nodes: 1 -> 2 -> 3 -> 4 -> 5 -> NULL. You want to insert a node with value 99 at position 6. What should happen and what will be the final list?
hard
A. Node 99 is appended at the end; list becomes 1 -> 2 -> 3 -> 4 -> 5 -> 99 -> NULL
B. Node 99 is inserted at position 5; list becomes 1 -> 2 -> 3 -> 4 -> 99 -> 5 -> NULL
C. Node 99 replaces node 5; list becomes 1 -> 2 -> 3 -> 4 -> 99 -> NULL
D. Insertion fails because position 6 is invalid; list remains unchanged
Solution
Step 1: Check list length and position
List has 5 nodes; position 6 means insert after last node (append).
Step 2: Insert node at end
Since position is one more than length, node 99 is added after node 5.
Final Answer:
Node 99 is appended at the end; list becomes 1 -> 2 -> 3 -> 4 -> 5 -> 99 -> NULL -> Option A
Quick Check:
Insert at pos > length means append [OK]
Hint: Position beyond length means append at end [OK]
Common Mistakes:
Rejecting valid append position
Replacing last node instead of appending
Inserting at wrong position due to off-by-one
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.