Insert a whole linked list into other at k-th position
Last Updated :
01 Sep, 2022
Given two linked lists and a number k. Insert second linked list in to first at k-th position
Examples:
Input : a : 1->2->3->4->5->NULL
b : 7->8->9->10->11->NULL
k = 2
Output :1->2->7->8->9->10->11->3->4->5->NULL
Input : a: 10->15->20->NULL
b: 11->17->16->18->NULL
k = 3
Output : 10->15->20->11->17->16->18->NULL
A pictorial representation of the problem
- Traverse the first linked list till k-th point
- Join second linked list head node to k-th point of first linked list
- Traverse the second linked list till end at
- Add (k+1)th point of first linked list to the end of the second linked list
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void insert( struct Node* head1, struct Node* head2,
int k)
{
int count = 1;
struct Node* curr = head1;
while (count < k) {
curr = curr->next;
count++;
}
struct Node* temp = curr->next;
curr->next = head2;
while (head2->next != NULL)
head2 = head2->next;
head2->next = temp;
}
void printList(Node* head)
{
if (head == NULL)
return ;
cout << head->data << " " ;
printList(head->next);
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* a = NULL;
struct Node* b = NULL;
int k = 2;
push(&a, 5);
push(&a, 4);
push(&a, 3);
push(&a, 2);
push(&a, 1);
push(&b, 11);
push(&b, 10);
push(&b, 9);
push(&b, 8);
push(&b, 7);
printList(a);
cout << "\n" ;
printList(b);
insert(a, b, k);
cout << "\nResulting linked list\t" ;
printList(a);
return 0;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
};
static Node insert(Node head1, Node head2,
int k)
{
int count = 1 ;
Node curr = head1;
while (count < k) {
curr = curr.next;
count++;
}
Node temp = curr.next;
curr.next = head2;
while (head2.next != null )
head2 = head2.next;
head2.next = temp;
return head1;
}
static void printList(Node head)
{
if (head == null )
return ;
System.out.print(head.data + " " );
printList(head.next);
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void main(String args[])
{
Node a = null ;
Node b = null ;
int k = 2 ;
a = push(a, 5 );
a = push(a, 4 );
a = push(a, 3 );
a = push(a, 2 );
a = push(a, 1 );
b = push(b, 11 );
b = push(b, 10 );
b = push(b, 9 );
b = push(b, 8 );
b = push(b, 7 );
printList(a);
System.out.println();
printList(b);
a = insert(a, b, k);
System.out.print( "\nResulting linked list\t" );
printList(a);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(head1, head2, k):
count = 1
curr = head1
while (count < k):
curr = curr. next
count + = 1
temp = curr. next
curr. next = head2
while (head2. next ! = None ):
head2 = head2. next
head2. next = temp
return head1
def printList(head):
if (head = = None ):
return
print ( head.data, end = " " )
printList(head. next )
def push(head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
if __name__ = = "__main__" :
a = None
b = None
k = 2
a = push(a, 5 )
a = push(a, 4 )
a = push(a, 3 )
a = push(a, 2 )
a = push(a, 1 )
b = push(b, 11 )
b = push(b, 10 )
b = push(b, 9 )
b = push(b, 8 )
b = push(b, 7 )
printList(a)
print ()
printList(b)
a = insert(a, b, k)
print ( "\nResulting linked list\t" , end = " " );
printList(a)
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node insert(Node head1, Node head2,
int k)
{
int count = 1;
Node curr = head1;
while (count < k) {
curr = curr.next;
count++;
}
Node temp = curr.next;
curr.next = head2;
while (head2.next != null )
head2 = head2.next;
head2.next = temp;
return head1;
}
static void printList(Node head)
{
if (head == null )
return ;
Console.Write(head.data + " " );
printList(head.next);
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void Main(String[] args)
{
Node a = null ;
Node b = null ;
int k = 2;
a = push(a, 5);
a = push(a, 4);
a = push(a, 3);
a = push(a, 2);
a = push(a, 1);
b = push(b, 11);
b = push(b, 10);
b = push(b, 9);
b = push(b, 8);
b = push(b, 7);
printList(a);
Console.WriteLine();
printList(b);
a = insert(a, b, k);
Console.Write( "\nResulting linked list\t" );
printList(a);
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .next = null ;
}
};
function insert(head1, head2, k)
{
var count = 1;
var curr = head1;
while (count < k) {
curr = curr.next;
count++;
}
var temp = curr.next;
curr.next = head2;
while (head2.next != null )
head2 = head2.next;
head2.next = temp;
return head1;
}
function printList(head)
{
if (head == null )
return ;
document.write(head.data + " " );
printList(head.next);
}
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
var a = null ;
var b = null ;
var k = 2;
a = push(a, 5);
a = push(a, 4);
a = push(a, 3);
a = push(a, 2);
a = push(a, 1);
b = push(b, 11);
b = push(b, 10);
b = push(b, 9);
b = push(b, 8);
b = push(b, 7);
printList(a);
document.write( "<br>" );
printList(b);
a = insert(a, b, k);
document.write( "<br>Resulting linked list " );
printList(a);
</script>
|
Output:
1 2 3 4 5
7 8 9 10 11
Resulting linked list 1 2 7 8 9 10 11 3 4 5
Time Complexity: O(k+n), as we are using a loop to traverse second linked list n times. Where n is the number of nodes in the second linked list to be inserted. and kth position in first linked list
Auxiliary Space: O(1) because it is using constant space
Share your thoughts in the comments
Please Login to comment...