0% completed
A linked list supports several fundamental operations that allow modification and traversal of elements efficiently. In this lesson, we will explore and implement the following operations:
- Traversal (Visiting each node sequentially)
- Insertion (Adding a node at the beginning, end, or middle)
- Deletion (Removing a node from the beginning, end, or middle)
Each operation modifies the structure of the linked list by updating pointers and maintaining connectivity.
1. Traversal in a Linked List
Traversal refers to the process of visiting each node in a linked list one by one. This operation is essential for reading data, searching for a value, or debugging a linked list. Unlike arrays, linked lists do not support direct access (like arr[i]
), so we must move through each node sequentially from the head.
Step-by-Step Algorithm for Traversal
- Start at the head of the linked list.
- While the current node is not NULL:
- Print or process the value stored in the current node.
- Move to the next node (
current = current.next
).
- Stop when you reach the last node (i.e.,
current == NULL
).
Algorithm Walkthrough
Code
Time and Space Complexity
✅ Time Complexity: O(n) (Each node is visited once).
✅ Space Complexity: O(1) (Only a single pointer is used).
2. Insertion in a Linked List
Insertion is the process of adding a new node at a specified position in a linked list. Since linked lists are dynamically allocated, insertion does not require shifting elements (as in arrays), but it requires updating pointers correctly.
We can insert a node at:
- The Beginning (Head Insertion) – Quickest insertion, updates the head pointer.
- The End (Tail Insertion) – Requires traversal to the last node.
- A Specific Position (Middle Insertion) – Requires traversal to find the correct location.
Step-by-Step Algorithm
- Create a new node with the given value and set its
next
pointer toNULL
. - If inserting at position 0 (beginning of the list):
- Set the
next
pointer of the new node to point to the currenthead
. - Update
head
to point to the new node. - The insertion is complete.
- Set the
- Otherwise, find the node at position (position - 1):
- Start from
head
and move forward until reaching the node before the desired position. - If the position is invalid (beyond the list length), print an error message and exit.
- Start from
- Insert the new node:
- Set
newNode.next
to point toprevious.next
(linking the new node to the next node). - Set
previous.next
to point to the new node (linking the previous node to the new node).
- Set
- Insertion is complete, and the linked list structure remains intact.
Algorithm Walkthrough
Code
Time and Space Complexity Analysis
Operation | Best Case Complexity | Worst Case Complexity | Average Case Complexity | Space Complexity |
---|---|---|---|---|
Insertion at Beginning | O(1) | O(1) | O(1) | O(1) |
Insertion at End | O(1) (if tail pointer exists) | O(n) | O(n) | O(1) |
Insertion at Middle | O(n) | O(n) | O(n) | O(1) |
- Insertion at the beginning is the fastest as it requires only pointer updates.
- Insertion at the end takes O(n) unless we maintain a tail pointer.
- Insertion at a specific position depends on how far we need to traverse, leading to O(n) complexity.
3. Deletion in a Linked List
Deletion in a linked list refers to removing a node from a specific position while maintaining the list’s structure. Unlike arrays, deletion in a linked list does not require shifting elements, but it requires updating pointers correctly.
A node can be deleted from:
- The Beginning (Head Deletion) – Quickest deletion, updates the head pointer.
- The End (Tail Deletion) – Requires traversal to find the second-last node.
- A Specific Position (Middle Deletion) – Requires traversal to locate the target node and update pointers.
The implementation should handle all three cases properly.
Step-by-Step Algorithm for Deletion
-
If the list is empty (
head == NULL
), print an error message and exit. -
If deleting the first node (position 0):
- Update
head
to point tohead.next
. - The deletion is complete.
- Update
-
Otherwise, find the node before the target position (
position - 1
):- Start from the
head
node. - Traverse the list using a loop for
position - 1
steps:- Move to the next node in each iteration.
- If the next node (
prev.next
) isNULL
before reaching the required position, exit the loop early.
- Start from the
-
If position is invalid (out of bounds):
- If
prev == NULL
orprev.next == NULL
, print"Invalid Position!"
and exit. - The position is beyond the list length, so deletion is not possible.
- If
-
Remove the target node:
- Update
prev.next
to point toprev.next.next
, effectively skipping the target node. - The node is successfully removed from the list.
- Update
Code
Time and Space Complexity Analysis
Operation | Best Case Complexity | Worst Case Complexity | Average Case Complexity | Space Complexity |
---|---|---|---|---|
Deletion at Beginning | O(1) | O(1) | O(1) | O(1) |
Deletion at End | O(n) | O(n) | O(n) | O(1) |
Deletion at Middle | O(n) | O(n) | O(n) | O(1) |
✅ Deletion at the beginning is the fastest as it requires only pointer updates.
✅ Deletion at the end takes O(n) unless we maintain a tail pointer.
✅ Deletion at a specific position depends on how far we need to traverse, leading to O(n) complexity.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible