- 1Singly Linked List Introduction
- 2Singly Linked List – Create Operation
- 3Singly Linked List – Insert Operation
- 4Singly Linked List – Delete Operation
- 5Singly Linked List – Traversal Operation
- 6Singly Linked List – Search Operation
- 7Singly Linked List – Get Length Operation
- 8Singly Linked List – Reverse Operation
- 9Doubly Linked List Introduction
- 10Doubly Linked List – Create Operation
- 11Doubly Linked List – Insert Operation
- 12Doubly Linked List – Delete Operation
- 13Doubly Linked List – Traversal Operation
- 14Doubly Linked List – Search Operation
- 15Doubly Linked List – Get Length Operation
- 16Doubly Linked List – Reverse Operation
Singly Linked List – Insert Operation
Insert Operation in Singly Linked List
Insertion in a singly linked list means placing a new node at a specific position. This can be at the beginning, in the middle, or at the end of the list.
The key part of insertion is updating the next
pointers correctly so that the new node is properly linked in the chain.
Visual Example
Let's understand how insertion works in a singly linked list by walking through an example step by step.
We start with the following list:
Now, we want to insert the value 30
at index 2. The final list should look like this:
Step-by-Step Insertion at Index 2
-
Start with the original list:
The list has three nodes with values:10
,20
, and40
. Each node points to the next one using anext
pointer. The last node points tonull
indicating the end of the list. -
Traverse the list to reach the node just before the insertion point:
Since we want to insert at index
2
, we need to find the node at index1
. That’s the node with value20
. We store a reference to this node in a temporary variable calledcurrent
. -
Create a new node with the value to be inserted:
30
. This node will be inserted between20
and40
. Initially, this new node does not point to anything. -
Set the new node’s
next
pointer:
We make the new node point to whatcurrent.next
was previously pointing to. Sincecurrent
is pointing to the node with value20
, andcurrent.next
was pointing to the node with value40
, now we do:
newNode.next = current.next
that is30 → 40
-
Link the previous node to the new node:
Now that the new node correctly points to the next part of the list, we update thecurrent
node (i.e., 20) so that it now points to the new node:
This completes the insertion. The linked list is now correctly updated.
Final Structure After Insertion
The list now looks like this: 10 → 20 → 30 → 40 → null
The green-highlighted node is the newly inserted node with value 30
.
Complete Pseudocode: Insert Node in Singly Linked List
This pseudocode shows how to insert a node at a specific index in a singly linked list.
function insertAt(head, index, value):
newNode = new ListNode(value)
if index == 0:
newNode.next = head
return newNode
curr = head
for i in range(index - 1):
curr = curr.next
newNode.next = curr.next
curr.next = newNode
return head
Let’s walk through each part of the code to understand what it does.
function insertAt(head, index, value):
newNode = new ListNode(value)
Explanation: We define a function that will insert a new node into the linked list. A new node is created using the value given as input.
if index == 0:
newNode.next = head
return newNode
Explanation: If the target index is 0, the new node should become the new head. We set the new node’s next
pointer to the current head and return the new node as the new head of the list.
curr = head
for i in range(index - 1):
curr = curr.next
Explanation: To insert elsewhere, we start at the head and move curr
to the node just before the target index. This is done by looping index - 1
times.
newNode.next = curr.next
curr.next = newNode
Explanation: After reaching the correct position, we first connect the new node to the next node using newNode.next
. Then, we connect the previous node (curr) to the new node using curr.next
.
return head
Explanation: Finally, we return the head of the updated list so the caller can continue using the modified list structure.
Singly Linked List Insert Operation Examples
The following examples demonstrate how to insert a new node at various positions in a singly linked list.
Initial List | Insert Index | Insert Value | Final Linked List | Explanation |
---|---|---|---|---|
[10, 20, 30] | 0 | 5 | 5 → 10 → 20 → 30 → null | Insert at the beginning. New node becomes the new head and points to the previous head. |
[10, 20, 30] | 2 | 25 | 10 → 20 → 25 → 30 → null | Insert in the middle. New node is placed between 20 and 30. |
[10, 20, 30] | 3 | 35 | 10 → 20 → 30 → 35 → null | Insert at the end. New node becomes the new tail and points to null . |
[10, 20, 30] | 5 | 50 | Error | Invalid index. The index is greater than the list size. Most implementations will throw an error or return failure. |
Time Complexity of Singly Linked List Insert Operation
Insertion Case | Time Complexity | Explanation |
---|---|---|
Insert at Beginning | O(1) | We directly update the head pointer to point to the new node. No traversal is required. |
Insert at Middle | O(n) | We must traverse the list to find the node just before the insertion point. This takes linear time. |
Insert at End (without tail pointer) | O(n) | We need to traverse from head to the last node to perform the insertion. |
Insert at End (with tail pointer) | O(1) | If we maintain a tail pointer, we can insert at the end directly without any traversal. |
Space Complexity of Singly Linked List Insert Operation
Insertion Case | Space Complexity | Explanation |
---|---|---|
Insert at Beginning | O(1) | Only one new node is created. No extra memory is required beyond that. |
Insert at Middle | O(1) | Only one new node is created and inserted into the existing structure. |
Insert at End | O(1) | Each insertion allocates space only for the new node. |
Key Concepts
- Head insertion: New node becomes the first node and points to old head.
- Middle insertion: Traverse to (index - 1), link new node to next, and rewire current node’s next.
- Tail insertion: Traverse to last node and attach new node to the end.
- Always update pointers carefully to avoid breaking the chain.
Comments
Loading comments...