A Linked List is a dynamic data structure that stores data using interconnected nodes. Each node contains data and a pointer that stores the address of the next node.
Creating a linked list is only the first step. To work with linked lists effectively, programmers perform various operations such as traversal, insertion, deletion, searching, and reversal. These operations allow data to be accessed, modified, and managed efficiently.
Understanding linked list operations is essential because they form the foundation of many advanced data structures and are frequently asked in coding interviews and technical assessments.
Table of Contents
Creating a Linked List
Before performing operations, we first need a linked list. Below is the C++ program to create a linked list:
// C++ program to create a linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
int main()
{
Node* first = new Node(10);
Node* second = new Node(20);
Node* third = new Node(30);
first-next = second;
second-next = third;
return 0;
}
Linked List Representation:
The above program creates three nodes and connects them to form a linked list.10 → 20 → 30 → NULL
Traversal in a Linked List
Traversal is the process of visiting every node of a linked list one by one. It is one of the most frequently used operations because many other operations, such as searching and updating data, depend on traversal.A temporary pointer starts from the head node and moves through the linked list until it reaches NULL. During this process, every node is visited exactly once.
Example:
Below is the C++ program for linked list traversal:Before traversal:
10 → 20 → 30 → NULL
Visited nodes:
10
20
30
// C++ program to implement linked list traversal
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
Node* temp = head;
while(temp != NULL)
{
cout temp-data " ";
temp = temp-next;
}
return 0;
}
Output:
Explanation:10 20 30
- A linked list containing three nodes is created.
- A temporary pointer named temp is initialized with the address of the head node.
- The while loop continues until temp becomes NULL.
- Each node’s data is displayed, and the pointer moves to the next node.
Space Complexity: O(1). No extra memory is used.
Insertion at the Beginning of a Linked List
Insertion at the beginning adds a new node before the current head node. After insertion, the newly added node becomes the first node of the linked list.This is one of the fastest insertion operations because only pointer references need to be updated.
Example:
Below is the C++ program to implement insertion at the beginning of the linked list:Before Insertion
10 → 20 → 30 → NULL
After Inserting 5
5 → 10 → 20 → 30 → NULL
// C++ program to implement insertion
// at the beginning of the linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
void display(Node* head)
{
Node* temp = head;
while(temp != NULL)
{
cout temp-data " ";
temp = temp-next;
}
cout endl;
}
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
Node* newNode = new Node(5);
newNode-next = head;
head = newNode;
display(head);
return 0;
}
Output:
Explanation:5 10 20 30
- Create the original linked list: 10 → 20 → 30 → NULL.
- Create a new node containing the value 5.
- Connect the new node to the current head: newNode-next = head;
- Update the head pointer: head = newNode;
- The new node becomes the first node: 5 → 10 → 20 → 30 → NULL.
Space Complexity: O(1). No extra memory is used apart from the new node.
Insertion at the End of a Linked List
Insertion at the end adds a new node after the last node of the linked list.To perform this operation, the list must first be traversed to locate the last node.
Example:
Below is the C++ program to implement insertion at the end of the linked list:Before Insertion
10 → 20 → 30 → NULL
After Inserting 40
10 → 20 → 30 → 40 → NULL
// C++ program to implement insertion at the
// end of the linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
void display(Node* head)
{
Node* temp = head;
while(temp != NULL)
{
cout temp-data " ";
temp = temp-next;
}
cout endl;
}
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
Node* newNode = new Node(40);
Node* temp = head;
while(temp-next != NULL)
{
temp = temp-next;
}
temp-next = newNode;
display(head);
return 0;
}
Output:10 20 30 40
Explanation:
- Create the linked list: 10 → 20 → 30 → NULL.
- Create a new node containing 40.
- Traverse the list until the last node is reached.
- Attach the new node after the last node: temp-next = newNode;
- The linked list becomes: 10 → 20 → 30 → 40 → NULL
Space Complexity: O(1). No additional memory is required.
Deletion at the Beginning of a Linked List
Deletion at the beginning removes the first node from the linked list. After deletion, the second node becomes the new head node.This operation is very efficient because only the head pointer needs to be updated.
Example:
Below is the C++ program to implement deletion at the beginning of the linked list:Before Deletion
10 → 20 → 30 → NULL
After Deletion
20 → 30 → NULL
// C++ program to implement deletion at the
// beginning of linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
void display(Node* head)
{
Node* temp = head;
while(temp != NULL)
{
cout temp-data " ";
temp = temp-next;
}
cout endl;
}
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
Node* temp = head;
head = head-next;
delete temp;
display(head);
return 0;
}
Output:
Explanation:20 30
- Create the linked list: 10 → 20 → 30 → NULL
- Store the address of the first node in a temporary pointer.
- Move the head pointer to the second node: head = head-next;
- Delete the old first node: delete temp;
- The linked list becomes: 20 → 30 → NULL
Space Complexity: O(1). No additional memory is required.
Deletion at the End of a Linked List
Deletion at the end removes the last node from the linked list. To perform this operation, the program must first locate the second-last node and then disconnect the last node.Unlike deletion at the beginning, this operation requires traversal because the last node cannot be accessed directly.
Example:
Below is the C++ program to implement deletion at the end of the linked list:Before Deletion
10 → 20 → 30 → NULL
After Deletion
10 → 20 → NULL
// C++ program to delete the node at
// the end of the linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
void display(Node* head)
{
Node* temp = head;
while(temp != NULL)
{
cout temp-data " ";
temp = temp-next;
}
cout endl;
}
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
Node* temp = head;
while(temp-next-next != NULL)
{
temp = temp-next;
}
delete temp-next;
temp-next = NULL;
display(head);
return 0;
}
Output:
Explanation:10 20
- Create the linked list: 10 → 20 → 30 → NULL
- Traverse the list until the second-last node is found.
- Delete the last node: delete temp-next;
- Update the second-last node’s next pointer: temp-next = NULL;
- The linked list becomes: 10 → 20 → NULL
Space Complexity: O(1). No extra memory is required.
Searching in a Linked List
Searching is the process of finding whether a particular element exists in the linked list. The search starts from the head node and continues until the required value is found or the end of the list is reached.Since linked lists do not support indexing like arrays, every node must be checked sequentially.
Example:
Below is the C++ program to implement search in a linked list:Linked List
10 → 20 → 30 → NULL
Search Element
20
// C++ program to implement searching in a
// linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
int key = 20;
bool found = false;
Node* temp = head;
while(temp != NULL)
{
if(temp-data == key)
{
found = true;
break;
}
temp = temp-next;
}
if(found)
cout "Element Found";
else
cout "Element Not Found";
return 0;
}
Output:
Explanation:Element Found
- Create the linked list: 10 → 20 → 30 → NULL
- Store the value to be searched: int key = 20;
- Traverse the linked list node by node.
- Compare every node’s value with the target value.
- Display the result when the element is found.
Space Complexity: O(1). No extra memory is used.
Finding the Length of a Linked List
Finding the length of a linked list means counting the total number of nodes present in the list.This operation is useful when performing position-based insertions, deletions, and validations.
Example:
Below is the C++ program to find the length of a linked list:Linked List
10 → 20 → 30 → 40 → NULL
// C++ program to find the length of
// a linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
head-next-next-next = new Node(40);
int count = 0;
Node* temp = head;
while(temp != NULL)
{
count++;
temp = temp-next;
}
cout "Length of Linked List = " count;
return 0;
}
Output:
Explanation:Length of Linked List = 4
- Initialize a counter variable with 0.
- Traverse the linked list from the head node.
- Increase the counter for every visited node.
- Display the final count.
Space Complexity: O(1). No extra memory is required.
Reversing a Linked List
Reversing a linked list changes the direction of all links between nodes. After reversal, the first node becomes the last node and the last node becomes the new head.This is one of the most frequently asked linked list interview questions because it tests understanding of pointers.
Example:
Below is the C++ program to reverse a linked list:Before Reversal
10 → 20 → 30 → NULL
After Reversal
30 → 20 → 10 → NULL
// C++ program to reverse a linked list
#include iostream
using namespace std;
class Node
{
public:
int data;
Node* next;
Node(int value)
{
data = value;
next = NULL;
}
};
void display(Node* head)
{
Node* temp = head;
while(temp != NULL)
{
cout temp-data " ";
temp = temp-next;
}
cout endl;
}
int main()
{
Node* head = new Node(10);
head-next = new Node(20);
head-next-next = new Node(30);
Node* prev = NULL;
Node* current = head;
Node* nextNode = NULL;
while(current != NULL)
{
nextNode = current-next;
current-next = prev;
prev = current;
current = nextNode;
}
head = prev;
display(head);
return 0;
}
Output:
Explanation:30 20 10
- Create the linked list: 10 → 20 → 30 → NULL.
- Use three pointers: prev, current, and nextNode.
- Reverse the direction of every link.
- Move all pointers one step forward.
- Update the head pointer: head = prev;
- The reversed linked list becomes: 30 → 20 → 10 → NULL
Space Complexity: O(1). Only a few extra pointers are used.
Conclusion
Linked list operations such as deletion, searching, length calculation, and reversal are essential for managing dynamic data efficiently. These operations help programmers manipulate data structures without requiring contiguous memory allocation.Understanding these concepts strengthens problem-solving skills, improves pointer handling, and prepares students for coding interviews and advanced DSA topics. Once these operations are mastered, solving complex linked list problems becomes much easier.
Frequently Asked Questions
1. Why is deletion at the end slower than deletion at the beginning?2. Why does searching take O(n) time?Deletion at the end requires traversal to find the second-last node, while deletion at the beginning only updates the head pointer.
3. Why is reversing a linked list a popular interview question?Linked lists do not support direct indexing, so nodes must be checked one by one.
4. Can linked lists grow dynamically?It tests understanding of pointers, node manipulation, and linked list traversal.
5. Which linked list operation is used most frequently?Yes. Nodes can be added and removed during program execution.
6. How is linked list length calculated?Traversal is the most commonly used operation because many other operations depend on it.
The list is traversed and every visited node is counted.
0 Comments