C++ Program For Inserting Node In The Middle Of The Linked List
Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node.
Examples:
Input : list: 1->2->4->5
x = 3
Output : 1->2->3->4->5
Input : list: 5->10->4->32->16
x = 41
Output : 5->10->4->41->32->16
Method 1(Using length of the linked list):
Find the number of nodes or length of the linked using one traversal. Let it be len. Calculate c = (len/2), if len is even, else c = (len+1)/2, if len is odd. Traverse again the first c nodes and insert the new node after the cth node.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtMid(Node** head_ref, int x)
{
if (*head_ref == NULL)
*head_ref = getNode(x);
else {
Node* newNode = getNode(x);
Node* ptr = *head_ref;
int len = 0;
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
int count = ((len % 2) == 0) ? (len / 2) :
(len + 1) / 2;
ptr = *head_ref;
while (count-- > 1)
ptr = ptr->next;
newNode->next = ptr->next;
ptr->next = newNode;
}
}
void display(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = NULL;
head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
cout << "Linked list before insertion: " ;
display(head);
int x = 3;
insertAtMid(&head, x);
cout << "
Linked list after insertion: ";
display(head);
return 0;
}
|
Output:
Linked list before insertion: 1 2 4 5
Linked list after insertion: 1 2 3 4 5
Time Complexity: O(n)
Space complexity: O(1) since using constant space
Method 2(Using two pointers):
Based on the tortoise and hare algorithm which uses two pointers, one known as slow and the other known as fast. This algorithm helps in finding the middle node of the linked list. It is explained in the front and black split procedure of this post. Now, you can insert the new node after the middle node obtained from the above process. This approach requires only a single traversal of the list.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAtMid(Node** head_ref, int x)
{
if (*head_ref == NULL)
*head_ref = getNode(x);
else {
Node* newNode = getNode(x);
Node* slow = *head_ref;
Node* fast = (*head_ref)->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
newNode->next = slow->next;
slow->next = newNode;
}
}
void display(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = NULL;
head = getNode(1);
head->next = getNode(2);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
cout << "Linked list before insertion: " ;
display(head);
int x = 3;
insertAtMid(&head, x);
cout << "
Linked list after insertion: ";
display(head);
return 0;
}
|
Output:
Linked list before insertion: 1 2 4 5
Linked list after insertion: 1 2 3 4 5
Time Complexity: O(n)
Space complexity: O(n) where n is size of linked list
Please refer complete article on Insert node into the middle of the linked list for more details!
Last Updated :
10 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...