✅ Insertion in Linked List
✅ 1. Introduction
Insertion in a linked list means adding a new node at a desired position in the list.
Since linked lists are dynamic, insertion can be done easily without shifting elements, unlike arrays.
✅ 2. Types of Insertion in Linked List
Insertion in a linked list can be performed at:
- Insertion at Beginning
- Insertion at End
- Insertion at a Specific Position (Middle)
These operations are explained below for a Singly Linked List.
✅ 3. Insertion at Beginning
✅ Definition
Insertion at beginning means adding a new node before the first node of the linked list.
✅ Steps Involved
- Create a new node
NEW - Store data in
NEW->data - Set
NEW->next = HEAD - Update
HEAD = NEW
✅ Diagram
Before insertion:
HEAD → 10 → 20 → 30 → NULL
After inserting 5:
HEAD → 5 → 10 → 20 → 30 → NULL
✅ Algorithm: Insert_Begin(HEAD, ITEM)
- Start
- Create new node
NEW - Set
NEW->data = ITEM - Set
NEW->next = HEAD - Set
HEAD = NEW - Stop
✅ Time Complexity
✅ O(1) (fastest insertion)
✅ 4. Insertion at End
✅ Definition
Insertion at end means adding a new node after the last node of the linked list.
✅ Steps Involved
- Create a new node
NEW - Set
NEW->data = ITEM - Set
NEW->next = NULL - Traverse the list to reach the last node
- Set
last->next = NEW
✅ Diagram
Before insertion:
HEAD → 10 → 20 → 30 → NULL
After inserting 40:
HEAD → 10 → 20 → 30 → 40 → NULL
✅ Algorithm: Insert_End(HEAD, ITEM)
- Start
- Create new node
NEW - Set
NEW->data = ITEM - Set
NEW->next = NULL - If
HEAD == NULL, then- Set
HEAD = NEWand Stop
- Set
- Set
temp = HEAD - While
temp->next != NULLtemp = temp->next
- Set
temp->next = NEW - Stop
✅ Time Complexity
✅ O(n) (traversal required)
✅ 5. Insertion at a Specific Position (Middle)
✅ Definition
Insertion at a specific position means adding a new node after a given node or at a given position in the linked list.
✅ Steps Involved
- Create new node
NEW - Traverse to the node just before the required position
- Set
NEW->next = temp->next - Set
temp->next = NEW
✅ Diagram
Insert 25 after 20:
Before:
HEAD → 10 → 20 → 30 → 40 → NULL
After:
HEAD → 10 → 20 → 25 → 30 → 40 → NULL
✅ Algorithm: Insert_Position(HEAD, ITEM, POS)
- Start
- Create new node
NEW - Set
NEW->data = ITEM - If
POS == 1, then- Perform insertion at beginning
- Set
temp = HEAD - Repeat
POS-2times:temp = temp->next
- Set
NEW->next = temp->next - Set
temp->next = NEW - Stop
✅ Time Complexity
✅ O(n)
✅ 6. Special Case: Insertion in Circular Linked List (Brief)
- Insertion at beginning or end requires updating last node pointer.
- No
NULLpointer exists.
✅ 7. Advantages of Insertion in Linked List
✅ Benefits:
- No shifting of elements
- Efficient insertion compared to arrays
- Dynamic memory usage
✅ Conclusion
Insertion in a linked list is a simple and efficient operation that involves adjusting pointers. Depending on the position, insertion can be done at the beginning (O(1)), end (O(n)), or middle (O(n)). Linked lists are preferred when frequent insertions are required.
