Given two linked lists, head1 and head2 of sizes m and n respectively. The task is to remove head1's nodes from the ath node to the bth node and insert head2 in their place.
Explanation: Remove the nodes from 3rd index till 4th index from head1 and insert head2 at their place.
Input: a = 0, b = 1 head1: 1 -> 2 , head2: 3 -> 4 Output: 3 -> 4 Explanation: Remove the nodes from 0th index till 1th index from head1 and insert head2 at their place.
Approach :
The idea is to start traversing from head1 and identify the node at position a - 1 and b. Let prev points to (a-1)th node and curr points to bth node in head1. Now update the pointer as following:
Update prev's next to point to head2 node as we need to remove some nodes in head1 after prev.
Start traversing head2 until we reach to end, and update the next pointer of the end node of head2 to curr's next. This will remove the node from a to b in head1.
C++
// C++ code to insert a linked list into // another linked list#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to merge head2 into head1 by removing nodes// from the ath to the bth position and inserting head2Node*mergeInBetween(Node*head1,inta,intb,Node*head2){Node*curr=head1;Node*prev=nullptr;// Traverse to the node just before position// 'a' and the node at position 'b'for(inti=0;curr!=nullptr;++i){if(i==a-1){// Node just before position 'a'prev=curr;}if(i==b){// Update the next pointer of the// node at position 'a - 1'prev->next=head2;// Traverse to the end of head2Node*endHead2=head2;while(endHead2->next!=nullptr){endHead2=endHead2->next;}// Connect the end of head2 to the // node after position 'b'endHead2->next=curr->next;break;}curr=curr->next;}returnhead1;}voidprintList(Node*head){Node*curr=head;while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}cout<<endl;}intmain(){// Creating head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15Node*head1=newNode(10);head1->next=newNode(11);head1->next->next=newNode(12);head1->next->next->next=newNode(13);head1->next->next->next->next=newNode(14);head1->next->next->next->next->next=newNode(15);// Creating head2: 100 -> 101 -> 102 -> 103Node*head2=newNode(100);head2->next=newNode(101);head2->next->next=newNode(102);head2->next->next->next=newNode(103);head1=mergeInBetween(head1,3,4,head2);printList(head1);return0;}
83
1
// C++ code to insert a linked list into
2
// another linked list
3
#include <iostream>
4
usingnamespacestd;
5
6
classNode {
7
public:
8
intdata;
9
Node*next;
10
11
Node(intx) {
12
data=x;
13
next=nullptr;
14
}
15
};
16
17
// Function to merge head2 into head1 by removing nodes
18
// from the ath to the bth position and inserting head2
// C code to insert a linked list into // another linked list#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Function to merge head2 into head1 by removing nodes // from the ath to the bth position and inserting head2structNode*mergeInBetween(structNode*head1,inta,intb,structNode*head2){structNode*curr=head1;structNode*prev=NULL;// Traverse to the node just before position 'a' and // the node at position 'b'for(inti=0;curr!=NULL;++i){if(i==a-1){// Node just before position 'a'prev=curr;}if(i==b){// Update the next pointer of the node// at position 'a - 1'prev->next=head2;// Traverse to the end of head2structNode*endHead2=head2;while(endHead2->next!=NULL){endHead2=endHead2->next;}// Connect the end of head2 to the // node after position 'b'endHead2->next=curr->next;break;}curr=curr->next;}returnhead1;}voidprintList(structNode*head){structNode*curr=head;while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}printf("\n");}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}intmain(){// Creating head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15structNode*head1=createNode(10);head1->next=createNode(11);head1->next->next=createNode(12);head1->next->next->next=createNode(13);head1->next->next->next->next=createNode(14);head1->next->next->next->next->next=createNode(15);// Creating head2: 100 -> 101 -> 102 -> 103structNode*head2=createNode(100);head2->next=createNode(101);head2->next->next=createNode(102);head2->next->next->next=createNode(103);head1=mergeInBetween(head1,3,4,head2);printList(head1);return0;}
Java
// Java code to insert a linked list// into another linked listclassNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}classGfG{// Function to merge head2 into head1 by removing nodes// from the ath to the bth position and inserting head2staticNodemergeInBetween(Nodehead1,inta,intb,Nodehead2){Nodecurr=head1;Nodeprev=null;// Traverse to the node just before position// 'a' and the node at position 'b'for(inti=0;curr!=null;++i){if(i==a-1){prev=curr;}if(i==b){// Update the next pointer of the node at// position 'a - 1'prev.next=head2;// Traverse to the end of head2NodeendHead2=head2;while(endHead2.next!=null){endHead2=endHead2.next;}// Connect the end of head2 to the node// after position 'b'endHead2.next=curr.next;break;}curr=curr.next;}returnhead1;}staticvoidprintList(Nodehead){Nodecurr=head;while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}System.out.println();}publicstaticvoidmain(String[]args){// Creating head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15Nodehead1=newNode(10);head1.next=newNode(11);head1.next.next=newNode(12);head1.next.next.next=newNode(13);head1.next.next.next.next=newNode(14);head1.next.next.next.next.next=newNode(15);// Creating head2: 100 -> 101 -> 102 -> 103Nodehead2=newNode(100);head2.next=newNode(101);head2.next.next=newNode(102);head2.next.next.next=newNode(103);head1=mergeInBetween(head1,3,4,head2);printList(head1);}}
Python
# Python code to insert a linked list# into another linked listclassNode:def__init__(self,data):self.data=dataself.next=Nonedefmerge_in_between(head1,a,b,head2):curr=head1prev=None# Traverse to the node just before # position 'a' and the node at position 'b'foriinrange(b+1):ifi==a-1:prev=currifi==b:# Update the next pointer of # the node at position 'a - 1'prev.next=head2# Traverse to the end of head2end_head2=head2whileend_head2.next:end_head2=end_head2.next# Connect the end of head2 # to the node after position 'b'end_head2.next=curr.nextbreakcurr=curr.nextreturnhead1defprint_list(head):curr=headwhilecurr:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Creating head1:# 10 -> 11 -> 12 -> 13 -> 14 -> 15head1=Node(10)head1.next=Node(11)head1.next.next=Node(12)head1.next.next.next=Node(13)head1.next.next.next.next=Node(14)head1.next.next.next.next.next=Node(15)# Creating head2: 100 -> 101 -> 102 -> 103head2=Node(100)head2.next=Node(101)head2.next.next=Node(102)head2.next.next.next=Node(103)head1=merge_in_between(head1,3,4,head2)print_list(head1)
C#
// C# code to insert a linked list// into another linked listusingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Function to merge head2 into head1 by removing nodes// from the ath to the bth position and inserting head2staticNodeMergeInBetween(Nodehead1,inta,intb,Nodehead2){Nodecurr=head1;Nodeprev=null;// Traverse to the node just before position 'a' and// the node at position 'b'for(inti=0;curr!=null;++i){if(i==a-1){prev=curr;}if(i==b){// Update the next pointer of the node at// position 'a - 1'prev.next=head2;// Traverse to the end of head2NodeendHead2=head2;while(endHead2.next!=null){endHead2=endHead2.next;}// Connect the end of head2 to the node// after position 'b'endHead2.next=curr.next;break;}curr=curr.next;}returnhead1;}staticvoidPrintList(Nodehead){Nodecurr=head;while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(){// Creating head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15Nodehead1=newNode(10);head1.next=newNode(11);head1.next.next=newNode(12);head1.next.next.next=newNode(13);head1.next.next.next.next=newNode(14);head1.next.next.next.next.next=newNode(15);// Creating head2: 100 -> 101 -> 102 -> 103Nodehead2=newNode(100);head2.next=newNode(101);head2.next.next=newNode(102);head2.next.next.next=newNode(103);head1=MergeInBetween(head1,3,4,head2);PrintList(head1);}}
JavaScript
// JavaScript code to insert a linked list into another// linked listclassNode{constructor(data){this.data=data;this.next=null;}}// Function to merge head2 into head1 by removing nodes// from the ath to the bth position and inserting head2functionmergeInBetween(head1,a,b,head2){letcurr=head1;letprev=null;// Traverse to the node just before position 'a' and the// node at position 'b'for(leti=0;curr!==null;++i){if(i===a-1){prev=curr;}if(i===b){// Update the next pointer of the node at// position 'a - 1'prev.next=head2;// Traverse to the end of head2letendHead2=head2;while(endHead2.next!==null){endHead2=endHead2.next;}// Connect the end of head2 to the node after// position 'b'endHead2.next=curr.next;break;}curr=curr.next;}returnhead1;}functionprintList(head){letcurr=head;while(curr!==null){console.log(curr.data+" ");curr=curr.next;}console.log();}// Creating head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15lethead1=newNode(10);head1.next=newNode(11);head1.next.next=newNode(12);head1.next.next.next=newNode(13);head1.next.next.next.next=newNode(14);head1.next.next.next.next.next=newNode(15);// Creating head2: 100 -> 101 -> 102 -> 103lethead2=newNode(100);head2.next=newNode(101);head2.next.next=newNode(102);head2.next.next.next=newNode(103);head1=mergeInBetween(head1,3,4,head2);printList(head1);
Output
10 11 12 100 101 102 103 15
Time Complexity: O(m + n), m is the length of head1 and n is the length of head2. Auxiliary Space: O(1)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.