Operations of Linked List
A linked list is a dynamic linear data structure made up of nodes connected through pointers. Each node contains data and the address of the next node. Unlike arrays, linked lists allow easy insertion and deletion of elements without shifting data.
To manage and manipulate the nodes in a linked list, several operations are performed. These operations help us add, remove, search, display, and modify data efficiently.
The main operations performed on a linked list are:
- Traversal
- Insertion
- Deletion
- Searching
- Updating
- Reversing
1. Traversal Operation
Traversal means visiting each node of the linked list one by one from the beginning to the end. It is one of the most fundamental operations because many other operations depend on traversal.
During traversal, we start from the head node and continue moving through the next pointers until the last node is reached. Since linked list nodes are stored at different memory locations, traversal is necessary to access every element.
Traversal is mainly used for:
- Displaying nodes
- Counting elements
- Processing data
Example
10 → 20 → 30 → 40 → NULLTraversal Output:
10 20 30 40Algorithm
TRAVERSE(head)
1. temp = head
2. While temp ≠ NULL
Print temp.data
temp = temp.next
3. Stop2. Insertion Operation
Insertion means adding a new node into the linked list. One of the biggest advantages of linked lists is that insertion can be performed easily without shifting elements.
Insertion can be done:
- At the beginning
- At the end
- At a specific position
A. Insertion at Beginning
In this operation, a new node is inserted before the first node of the list.
The new node becomes the first node, and the head pointer is updated to point to the new node.
Example
Before insertion:
20 → 30 → NULLAfter inserting 10:
10 → 20 → 30 → NULLWorking Process
- Create a new node
- Store data in the node
- Point the new node to the current head
- Move head to the new node
Algorithm
INSERT_BEGIN(head, value)
1. Create new node
2. new.data = value
3. new.next = head
4. head = newB. Insertion at End
This operation inserts a node after the last node of the list.
To perform this operation, the list is traversed until the last node is found. Then the last node is connected to the new node.
Example
Before insertion:
10 → 20 → NULLAfter inserting 30:
10 → 20 → 30 → NULLWorking Process
- Create a new node
- Traverse to the last node
- Connect last node to new node
- Set new node next to NULL
Algorithm
INSERT_END(head, value)
1. Create new node
2. new.next = NULL
3. Traverse to last node
4. last.next = newC. Insertion at Specific Position
In this method, a node is inserted at a desired location in the linked list.
The node before the desired position is located first. Then the pointers are adjusted carefully to insert the new node.
Example
Before insertion:
10 → 30 → 40 → NULLInsert 20 at position 2:
10 → 20 → 30 → 40 → NULLWorking Process
- Move to desired position
- Create new node
- Adjust pointers
- Insert node
Algorithm
INSERT_POSITION(head, value, pos)
1. Move to position-1
2. Create new node
3. new.next = temp.next
4. temp.next = new3. Deletion Operation
Deletion means removing a node from the linked list. In linked lists, deletion is easier than arrays because elements do not need to be shifted.
Deletion can be performed:
- From the beginning
- From the end
- From a specific position
A. Deletion from Beginning
The first node of the linked list is removed.
The head pointer is updated to point to the second node.
Example
Before deletion:
10 → 20 → 30 → NULLAfter deletion:
20 → 30 → NULLWorking Process
- Store current head
- Move head to next node
- Delete old first node
Algorithm
DELETE_BEGIN(head)
1. temp = head
2. head = head.next
3. Delete tempB. Deletion from End
This operation removes the last node from the linked list.
The second-last node becomes the new last node.
Example
Before deletion:
10 → 20 → 30 → NULLAfter deletion:
10 → 20 → NULLWorking Process
- Traverse to second-last node
- Remove last node
- Set next pointer to NULL
Algorithm
DELETE_END(head)
1. Move to second-last node
2. Delete last node
3. temp.next = NULLC. Deletion of Specific Node
This operation removes a node with a particular value or position.
Example
Before deletion:
10 → 20 → 30 → 40 → NULLDelete 30:
10 → 20 → 40 → NULLWorking Process
- Search target node
- Connect previous node to next node
- Remove target node
Algorithm
DELETE_NODE(head, key)
1. Search for key node
2. Change links
3. Delete target node4. Searching Operation
Searching means finding whether a particular element exists in the linked list.
Since linked lists do not support direct indexing, each node must be checked sequentially until the desired element is found.
Example
10 → 20 → 30 → 40 → NULLSearch Key = 30
Result: Element Found
Working Process
- Start from head
- Compare each node
- Continue until element is found or NULL reached
Algorithm
SEARCH(head, key)
1. temp = head
2. While temp ≠ NULL
If temp.data = key
Return Found
temp = temp.next
3. Return Not Found5. Updating Operation
Updating means modifying the value stored in a node.
Only the data part changes; the structure of the linked list remains the same.
Example
Before update:
10 → 20 → 30 → NULLUpdate 20 to 25:
10 → 25 → 30 → NULLWorking Process
- Search target node
- Replace old value with new value
Algorithm
UPDATE(head, oldValue, newValue)
1. Search oldValue
2. Replace with newValue6. Reversing Operation
Reversing changes the direction of the linked list.
After reversing:
- The last node becomes the first node
- The first node becomes the last node
This operation changes all pointer directions.
Example
Before reversing:
10 → 20 → 30 → NULLAfter reversing:
30 → 20 → 10 → NULLWorking Process
- Reverse pointer direction
- Update head to last node
Algorithm
REVERSE(head)
1. prev = NULL
2. current = head
3. While current ≠ NULL
next = current.next
current.next = prev
prev = current
current = next
4. head = prevTime Complexity of Linked List Operations
| Operation | Time Complexity |
|---|---|
| Traversal | O(n) |
| Insertion at Beginning | O(1) |
| Insertion at End | O(n) |
| Deletion at Beginning | O(1) |
| Searching | O(n) |
| Reversing | O(n) |
Applications of Linked List Operations
Linked list operations are widely used in:
- Music playlist systems
- Browser history
- Undo and redo operations
- Memory management
- Dynamic data storage
- Graph implementation
Conclusion
Operations on linked lists are essential for dynamic data management. Traversal helps access nodes, insertion and deletion manage changes in the structure, searching locates required data, updating modifies node values, and reversing changes the order of the list. Because of these flexible operations, linked lists are widely used in operating systems, applications, and advanced data structures.
Was this article helpful?