DSA Tutorial



OPERATIONS ON LINKED LIST


🔄 Operations on Linked List

A Linked List supports a variety of operations that allow us to manipulate and work with the list. The most common operations on a linked list include:

1. Insertion in Linked List

Insertion involves adding a new node to the list. Nodes can be added in three positions:

  • At the beginning: A new node is added before the first node (head).
  • At the end: A new node is added after the last node (tail).
  • At a specific position: A new node is inserted at any given index in the list.
🔄 Example:
Head → Node 1 → Node 2 → Node 3 → NULL
After insertion at the beginning:
New Node → Head → Node 1 → Node 2 → Node 3 → NULL

2. Deletion in Linked List

Deletion removes a node from the linked list. Deletion can happen in the following ways:

  • From the beginning: The first node (head) is removed.
  • From the end: The last node is removed, and the previous node's next pointer is set to NULL.
  • From a specific position: Any node can be deleted by updating the previous node's next pointer.
🔄 Example:
Head → Node 1 → Node 2 → Node 3 → NULL
After deletion of Node 1:
Head → Node 2 → Node 3 → NULL

3. Traversal in Linked List

Traversal involves visiting each node in the linked list. This operation is commonly used to print the list or search for specific elements. It can be done from the head to the tail in a singly linked list and in both directions in a doubly linked list.

🔄 Example:
Head → Node 1 → Node 2 → Node 3 → NULL
Traversal: Node 1 → Node 2 → Node 3

4. Searching in Linked List

Searching involves finding a node with a specific value in the list. It can be done by traversing the list and checking each node's value until the desired element is found or the end of the list is reached.

🔄 Example:
Head → Node 1 → Node 2 → Node 3 → NULL
Search for Node 2: Found Node 2

5. Updating Node in Linked List

Update allows modification of the data in any existing node. This is done by accessing the node and changing its value.

🔄 Example:
Head → Node 1 → Node 2 → Node 3 → NULL
After update of Node 2 value to 5:
Head → Node 1 → Node 5 → Node 3 → NULL

6. Reversing Linked List

Reversing a linked list means changing the direction of the links so that the head becomes the tail and vice versa. This operation is useful for reversing the order of elements.

🔄 Example:
Head → Node 1 → Node 2 → Node 3 → NULL
After reversing:
Node 3 → Node 2 → Node 1 → Head
🚀 Advantages of Linked List Operations:
- Dynamic memory allocation: Linked lists can grow and shrink in size during runtime.
- Efficient insertions and deletions: No need to shift elements as in arrays.
- Flexible structure: Linked lists can easily adapt to changes in data.
🚀 Try Implementing Linked List Operations:
- Implement insertions at the beginning, end, and specific positions.
- Try deleting nodes from the beginning, end, and specific positions.
- Implement a reverse function and see how the list changes.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review