Given two linked lists, insert nodes of second list into first list at alternate positions of first list.
For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become 5->12->7->10->17->2->13->4->11->6 and second list should become empty. The nodes of second list should only be inserted when there are positions available. For example, if the first list is 1->2->3 and second list is 4->5->6->7->8, then first list should become 1->4->2->5->3->6 and second list to 7->8.
Use of extra space is not allowed (Not allowed to create additional nodes), i.e., insertion must be done in-place. Expected time complexity is O(n) where n is number of nodes in first list.
The idea is to run a loop while there are available positions in first loop and insert nodes of second list by changing pointers.
Following are implementations of this approach.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void 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;
}
void printList(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void merge(Node *p, Node **q)
{
Node *p_curr = p, *q_curr = *q;
Node *p_next, *q_next;
while (p_curr != NULL && q_curr != NULL)
{
p_next = p_curr->next;
q_next = q_curr->next;
q_curr->next = p_next;
p_curr->next = q_curr;
p_curr = p_next;
q_curr = q_next;
}
*q = q_curr;
}
int main()
{
Node *p = NULL, *q = NULL;
push(&p;, 3);
push(&p;, 2);
push(&p;, 1);
cout<<"First Linked List:\n";
printList(p);
push(&q;, 8);
push(&q;, 7);
push(&q;, 6);
push(&q;, 5);
push(&q;, 4);
cout<<"Second Linked List:\n";
printList(q);
merge(p, &q;);
cout<<"Modified First Linked List:\n";
printList(p);
cout<<"Modified Second Linked List:\n";
printList(q);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void push(struct Node ** head_ref, int new_data)
{
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void merge(struct Node *p, struct Node **q)
{
struct Node *p_curr = p, *q_curr = *q;
struct Node *p_next, *q_next;
while (p_curr != NULL && q_curr != NULL)
{
p_next = p_curr->next;
q_next = q_curr->next;
q_curr->next = p_next;
p_curr->next = q_curr;
p_curr = p_next;
q_curr = q_next;
}
*q = q_curr;
}
int main()
{
struct Node *p = NULL, *q = NULL;
push(&p;, 3);
push(&p;, 2);
push(&p;, 1);
printf("First Linked List:\n");
printList(p);
push(&q;, 8);
push(&q;, 7);
push(&q;, 6);
push(&q;, 5);
push(&q;, 4);
printf("Second Linked List:\n");
printList(q);
merge(p, &q;);
printf("Modified First Linked List:\n");
printList(p);
printf("Modified Second Linked List:\n");
printList(q);
getchar();
return 0;
}
|
Java
class LinkedList
{
Node head;
class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}
void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void merge(LinkedList q)
{
Node p_curr = head, q_curr = q.head;
Node p_next, q_next;
while (p_curr != null && q_curr != null) {
p_next = p_curr.next;
q_next = q_curr.next;
q_curr.next = p_next;
p_curr.next = q_curr;
p_curr = p_next;
q_curr = q_next;
}
q.head = q_curr;
}
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
llist1.push(3);
llist1.push(2);
llist1.push(1);
System.out.println("First Linked List:");
llist1.printList();
llist2.push(8);
llist2.push(7);
llist2.push(6);
llist2.push(5);
llist2.push(4);
System.out.println("Second Linked List:");
llist1.merge(llist2);
System.out.println("Modified first linked list:");
llist1.printList();
System.out.println("Modified second linked list:");
llist2.printList();
}
}
|
Python3
class Node(object):
def __init__(self, data:int):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = None
def push(self, new_data:int):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def printList(self):
temp = self.head
while temp != None:
print(temp.data)
temp = temp.next
def merge(self, p, q):
p_curr = p.head
q_curr = q.head
while p_curr != None and q_curr != None:
p_next = p_curr.next
q_next = q_curr.next
q_curr.next = p_next
p_curr.next = q_curr
p_curr = p_next
q_curr = q_next
q.head = q_curr
llist1 = LinkedList()
llist2 = LinkedList()
llist1.push(3)
llist1.push(2)
llist1.push(1)
llist1.push(0)
for i in range(8, 3, -1):
llist2.push(i)
print("First Linked List:")
llist1.printList()
print("Second Linked List:")
llist2.printList()
llist1.merge(p=llist1, q=llist2)
print("Modified first linked list:")
llist1.printList()
print("Modified second linked list:")
llist2.printList()
|
C#
using System;
public class LinkedList
{
Node head;
public class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}
}
void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void merge(LinkedList q)
{
Node p_curr = head, q_curr = q.head;
Node p_next, q_next;
while (p_curr != null && q_curr != null)
{
p_next = p_curr.next;
q_next = q_curr.next;
q_curr.next = p_next;
p_curr.next = q_curr;
p_curr = p_next;
q_curr = q_next;
}
q.head = q_curr;
}
void printList()
{
Node temp = head;
while (temp != null)
{
Console.Write(temp.data+" ");
temp = temp.next;
}
Console.WriteLine();
}
public static void Main()
{
LinkedList llist1 = new LinkedList();
LinkedList llist2 = new LinkedList();
llist1.push(3);
llist1.push(2);
llist1.push(1);
Console.WriteLine("First Linked List:");
llist1.printList();
llist2.push(8);
llist2.push(7);
llist2.push(6);
llist2.push(5);
llist2.push(4);
Console.WriteLine("Second Linked List:");
llist1.merge(llist2);
Console.WriteLine("Modified first linked list:");
llist1.printList();
Console.WriteLine("Modified second linked list:");
llist2.printList();
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this.data = 0;
this.next = null;
}
};
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;
}
function printList(head)
{
var temp = head;
while (temp != null)
{
document.write( temp.data + " ");
temp = temp.next;
}
document.write("<br>");
}
function merge(p, q)
{
var p_curr = p, q_curr = q;
var p_next, q_next;
while (p_curr != null && q_curr != null)
{
p_next = p_curr.next;
q_next = q_curr.next;
q_curr.next = p_next;
p_curr.next = q_curr;
p_curr = p_next;
q_curr = q_next;
}
q = q_curr;
return q;
}
var p = null, q = null;
p = push(p, 3);
p = push(p, 2);
p = push(p, 1);
document.write( "First Linked List:<br>");
printList(p);
q = push(q, 8);
q = push(q, 7);
q = push(q, 6);
q = push(q, 5);
q = push(q, 4);
document.write( "Second Linked List:<br>");
printList(q);
q = merge(p, q);
document.write( "Modified First Linked List:<br>");
printList(p);
document.write( "Modified Second Linked List:<br>");
printList(q);
</script>
|
OutputFirst Linked List:
1 2 3
Second Linked List:
4 5 6 7 8
Modified First Linked List:
1 4 2 5 3 6
Modified Second Linked List:
7 8
Time Complexity: O(min(n1, n2)), where n1 and n2 represents the length of the given two linked lists.
Auxiliary Space: O(1), no extra space is required, so it is a constant.