Linked Lists Programs
Linked Lists Programs
To insert a node at the front of the linked list, follow these steps:
Create a new node.
Set the next pointer of the new node to the current head of the list.
Update the head pointer to the new node.
Below is the implementation of the approach:
C++
// C++ Program to insert the node at the beginning of
// Linked List
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node(int new_data)
{
data = new_data;
next = nullptr;
}
};
int main()
{
// Create the linked list 2->3->4->5
Node* head = new Node(2);
head->next = new Node(3);
head->next->next = new Node(4);
head->next->next->next = new Node(5);
return 0;
}
Output
Original Linked List: 2 3 4 5
After inserting Nodes at the front: 1 2 3 4 5
Time Complexity: O(1), We have a pointer to the head and we can
directly attach a node and update the head pointer. So, the Time
complexity of inserting a node at the head position is O(1).
Auxiliary Space: O(1)
2. Insert a Node after a Given Node in Linked List
If we want to insert a new node after a specific node, we first locate that
node. Once we find it, we set the new node’s next reference to point to
the node that follows the given node. Then, we update the given node’s
next to point to the new node. This requires traversing the list to find the
specified node.
struct Node {
int data;
Node* next;
Node(int value)
{
data = value;
next = nullptr;
}
};
int main()
{
// Create the linked list 2->3->4->5
Node* head = new Node(2);
head->next = new Node(3);
head->next->next = new Node(5);
head->next->next->next = new Node(6);
return 0;
}
Output
Original Linked List: 2 3 5 6
Linked List after insertion: 2 3 4 5 6
Time complexity: O(N), where N is the number of nodes in the linked list.
Since there is a loop from head to end, the function does O(n) work.
Auxiliary Space: O(1)
3. Insert a Node At a Specific Position in Linked List
To insert a new node at a specific position, we need to traverse the list to
position – 1. If the position is valid, we adjust the pointers similarly such
that the next pointer of the new node points to the next of current
nod and next pointer of current node points to the new node.
struct Node {
int data;
Node* next;
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
int main()
{
// Creating the list 3->5->8->10
Node* head = new Node(3);
head->next = new Node(5);
head->next->next = new Node(8);
head->next->next->next = new Node(10);
return 0;
}
Output
Linked list before insertion: 3 5 8 10
Linked list after insertion of 12 at position 3: 3 12 5 8 10
Time complexity: O(N), where N is the number of nodes in the linked list.
Since there is a loop from head to end, the function does O(n) work.
Auxiliary Space: O(1)
4. Insert a Node at the End of Linked List
Inserting at the end involves traversing the entire list until we reach the
last node. We then set the last node’s next reference to point to the new
node, making the new node the last element in the list.
class Node {
public:
int data;
Node* next;
Node(int new_data)
{
data = new_data;
next = nullptr;
}
};
int main()
{
// Create a linked list: 2->3->4->5->6
Node* head = new Node(2);
head->next = new Node(3);
head->next->next = new Node(4);
head->next->next->next = new Node(5);
head->next->next->next->next = new Node(6);
return 0;
}
Output
Created Linked list is: 2 3 4 5 6
After inserting 1 at the end: 2 3 4 5 6 1
Time complexity: O(N), where N is the number of nodes in the linked list.
Since there is a loop from head to end, the function does O(n) work.
Auxiliary Space: O(1)
1. Delete the Head Node
When deleting the head node, we need to adjust the head pointer to point
to the next node otherwise, the whole linked list can be lost.
Following is the approach to remove the head node of the linked list:
Store the current head node in a temporary variable.
Move the head pointer to the next node.
Delete the temporary node.
Implementation
C++
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
return 0;
}
Output
Original List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 12 -> 15 -> 18 -> NULL
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Delete the Tail Node
To delete the tail node, traverse the list to find the second-to-last node,
and then set its next pointer to NULL.
Following is the approach to remove the head node of the linked list:
Traverse the list to find the second-to-last node.
Set the next pointer of the second-to-last node to NULL.
Delete the last node.
Implementation
C++
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
int main() {
return 0;
}
Output
Original List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 3 -> 12 -> 15 -> NULL
Time Complexity: O(N), where N is the number of nodes in the list.
Auxiliary Space: O (1)
3. Delete a Node at a Specific Position
To delete a node at a specific position, traverse the list to reach the node
just before the target position, adjust pointers to skip the target node, and
delete it.
Following is the approach to remove the head node of the linked list:
If the position is 1, delete the head. Otherwise,
Traverse the list to reach the node before the target position.
Adjust pointers to bypass the target node and point to then next of the
target node.
Delete the target node.
Implementation
C++
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
int main() {
return 0;
}
Output
Original List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 3 -> 15 -> 18 -> NULL
Time Complexity: O(N), where N is the number of nodes in the list.
Auxiliary Space: O (1)
Conclusion
Deleting a node from a linked list is a common operation that requires
careful handling of pointers to avoid breaking the list structure. Depending
on the position of the node to be deleted (head, tail, or a specific position),
the approach varies slightly. These examples demonstrate how to handle
different deletion scenarios in a singly linked list effectively.
Here's a C++ program that implements a menu-driven system to maintain a linear linked list
with elements stored in ascending order. The program includes separate functions for
inserting, deleting, searching, and displaying the elements of the list.
#include <iostream>
using namespace std;
if (!current->next) {
cout << "Element not found.\n";
return;
}
do {
cout << "\nMenu:\n";
cout << "1. Insert an element\n";
cout << "2. Delete an element\n";
cout << "3. Search for an element\n";
cout << "4. Display all elements\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
insertElement(head, value);
break;
case 2:
cout << "Enter value to delete: ";
cin >> value;
deleteElement(head, value);
break;
case 3:
cout << "Enter value to search: ";
cin >> value;
searchElement(head, value);
break;
case 4:
displayElements(head);
break;
case 5:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}
Explanation:
This program will maintain the linked list in ascending order and allow the user to perform
the required operations interactively.