Create a linked list from two linked lists by choosing max element at each position
Last Updated :
05 Feb, 2023
Given two linked list of equal sizes, the task is to create new linked list using those linked lists where at every step, the maximum of the two elements from both the linked lists is chosen and the other is skipped.
Examples:
Input:
list1 = 5 -> 2 -> 3 -> 8 -> NULL
list2 = 1 -> 7 -> 4 -> 5 -> NULL
Output: 5 -> 7 -> 4 -> 8 -> NULL
Input:
list1 = 2 -> 8 -> 9 -> 3 -> NULL
list2 = 5 -> 3 -> 6 -> 4 -> NULL
Output: 5 -> 8 -> 9 -> 4 -> NULL
A linked list is a data structure where each element is connected to the next element in the list. In this task, we have two linked lists and we want to create a new linked list by picking the maximum element from each position of the two original linked lists and placing it in the same position in the new linked list.
For example, let’s say we have two linked lists:
List 1: [1, 3, 5, 7]
List 2: [2, 4, 6, 8]
To create the new linked list, we compare the first elements of both original linked lists, which are 1 and 2. We pick the maximum of these two, which is 2, and place it in the first position of the new linked list.
Next, we compare the second elements of both original linked lists, which are 3 and 4. We pick the maximum of these two, which is 4, and place it in the second position of the new linked list.
We repeat this process for all elements in both linked lists, until we have gone through all elements in both linked lists.
So, the new linked list would be: [2, 4, 6, 8]
This is how we can create a new linked list by choosing the maximum element at each position from two linked lists.
Approach: We traverse both the linked list at the same time and compare node from both the lists. The node which is greater among them, will be added to the new linked list. We do this for each node and then print the nodes of the generated linked list.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insert(Node** root, int item)
{
Node *ptr, *temp;
temp = new Node;
temp->data = item;
temp->next = NULL;
if (*root == NULL)
*root = temp;
else {
ptr = *root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void display(Node* root)
{
while (root != NULL) {
cout << root->data << " -> " ;
root = root->next;
}
cout << "NULL" ;
}
Node* newList(Node* root1, Node* root2)
{
Node *ptr1 = root1, *ptr2 = root2;
Node* root = NULL;
while (ptr1 != NULL) {
int currMax = ((ptr1->data < ptr2->data)
? ptr2->data
: ptr1->data);
if (root == NULL) {
Node* temp = new Node;
temp->data = currMax;
temp->next = NULL;
root = temp;
}
else {
insert(&root, currMax);
}
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return root;
}
int main()
{
Node *root1 = NULL, *root2 = NULL, *root = NULL;
insert(&root1, 5);
insert(&root1, 2);
insert(&root1, 3);
insert(&root1, 8);
insert(&root2, 1);
insert(&root2, 7);
insert(&root2, 4);
insert(&root2, 5);
root = newList(root1, root2);
display(root);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node insert(Node root, int item)
{
Node ptr, temp;
temp = new Node();
temp.data = item;
temp.next = null ;
if (root == null )
root = temp;
else
{
ptr = root;
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return root;
}
static void display(Node root)
{
while (root != null )
{
System.out.print( root.data + " - > " );
root = root.next;
}
System.out.print( "null" );
}
static Node newList(Node root1, Node root2)
{
Node ptr1 = root1, ptr2 = root2;
Node root = null ;
while (ptr1 != null )
{
int currMax = ((ptr1.data < ptr2.data)
? ptr2.data
: ptr1.data);
if (root == null )
{
Node temp = new Node();
temp.data = currMax;
temp.next = null ;
root = temp;
}
else
{
root = insert(root, currMax);
}
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return root;
}
public static void main(String args[])
{
Node root1 = null , root2 = null , root = null ;
root1 = insert(root1, 5 );
root1 = insert(root1, 2 );
root1 = insert(root1, 3 );
root1 = insert(root1, 8 );
root2 = insert(root2, 1 );
root2 = insert(root2, 7 );
root2 = insert(root2, 4 );
root2 = insert(root2, 5 );
root = newList(root1, root2);
display(root);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def insert(root, item):
temp = Node(item)
temp.data = item
temp. next = None
if (root = = None ):
root = temp
else :
ptr = root
while (ptr. next ! = None ):
ptr = ptr. next
ptr. next = temp
return root
def display(root):
while (root ! = None ) :
print (root.data, end = "->" )
root = root. next
print ( "NULL" )
def newList(root1, root2):
ptr1 = root1
ptr2 = root2
root = None
while (ptr1 ! = None ) :
currMax = ((ptr1.data < ptr2.data) and
ptr2.data or ptr1.data)
if (root = = None ):
temp = Node(currMax)
temp.data = currMax
temp. next = None
root = temp
else :
root = insert(root, currMax)
ptr1 = ptr1. next
ptr2 = ptr2. next
return root
if __name__ = = '__main__' :
root1 = None
root2 = None
root = None
root1 = insert(root1, 5 )
root1 = insert(root1, 2 )
root1 = insert(root1, 3 )
root1 = insert(root1, 8 )
root2 = insert(root2, 1 )
root2 = insert(root2, 7 )
root2 = insert(root2, 4 )
root2 = insert(root2, 5 )
root = newList(root1, root2)
display(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node insert(Node root, int item)
{
Node ptr, temp;
temp = new Node();
temp.data = item;
temp.next = null ;
if (root == null )
root = temp;
else
{
ptr = root;
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return root;
}
static void display(Node root)
{
while (root != null )
{
Console.Write( root.data + " - > " );
root = root.next;
}
Console.Write( "null" );
}
static Node newList(Node root1, Node root2)
{
Node ptr1 = root1, ptr2 = root2;
Node root = null ;
while (ptr1 != null )
{
int currMax = ((ptr1.data < ptr2.data)
? ptr2.data
: ptr1.data);
if (root == null )
{
Node temp = new Node();
temp.data = currMax;
temp.next = null ;
root = temp;
}
else
{
root = insert(root, currMax);
}
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return root;
}
public static void Main(String []args)
{
Node root1 = null , root2 = null , root = null ;
root1 = insert(root1, 5);
root1 = insert(root1, 2);
root1 = insert(root1, 3);
root1 = insert(root1, 8);
root2 = insert(root2, 1);
root2 = insert(root2, 7);
root2 = insert(root2, 4);
root2 = insert(root2, 5);
root = newList(root1, root2);
display(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function insert( root , item)
{
var ptr, temp;
temp = new Node();
temp.data = item;
temp.next = null ;
if (root == null )
root = temp;
else {
ptr = root;
while (ptr.next != null )
ptr = ptr.next;
ptr.next = temp;
}
return root;
}
function display( root)
{
while (root != null )
{
document.write(root.data + " - > " );
root = root.next;
}
document.write( "null" );
}
function newList( root1, root2) {
ptr1 = root1, ptr2 = root2;
root = null ;
while (ptr1 != null ) {
var currMax = ((ptr1.data < ptr2.data) ? ptr2.data : ptr1.data);
if (root == null ) {
temp = new Node();
temp.data = currMax;
temp.next = null ;
root = temp;
}
else {
root = insert(root, currMax);
}
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return root;
}
root1 = null , root2 = null , root = null ;
root1 = insert(root1, 5);
root1 = insert(root1, 2);
root1 = insert(root1, 3);
root1 = insert(root1, 8);
root2 = insert(root2, 1);
root2 = insert(root2, 7);
root2 = insert(root2, 4);
root2 = insert(root2, 5);
root = newList(root1, root2);
display(root);
</script>
|
Output:
5 -> 7 -> 4 -> 8 -> NULL
Time complexity: O(n) where n is size of linked list
Space complexity: O(n) where n is size of newly created linked list
Similar Reads
Create new linked list from two given linked list with greater element at each node
Given two linked list of the same size, the task is to create a new linked list using those linked lists. The condition is that the greater node among both linked list will be added to the new linked list.Examples: Input: list1 = 5->2->3->8list2 = 1->7->4->5Output: New list = 5-
8 min read
Merge a linked list into another linked list at alternate positions
Given two singly linked lists, The task is to insert nodes of the second list into the first list at alternate positions of the first list and leave the remaining nodes of the second list if it is longer. Example: Input: head1: 1->2->3 , head2: 4->5->6->7->8Output: head1: 1->4-
8 min read
Javascript Program To Merge A Linked List Into Another Linked List At Alternate Positions
Given two linked lists, insert nodes of the second list into the first list at alternate positions of the 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-
3 min read
Insert a whole linked list into other at k-th position
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
10 min read
Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
Given two sorted linked lists, construct a linked list that contains maximum sum path from start to end. The result list may contain nodes from both input lists. When constructing the result list, we may switch to the other input list only at the point of intersection (which mean the two node with t
14 min read
How to create a pointer to another pointer in a linked list?
In this article we cover how to create a pointer to another pointer in a linked list, it can be singly, doubly, or circularly. What is Linked List: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked
7 min read
XOR Linked List â A Memory Efficient Doubly Linked List | Set 2
In the previous post, we discussed how a Doubly Linked can be created using only one space for the address field with every node. In this post, we will discuss the implementation of a memory-efficient doubly linked list. We will mainly discuss the following two simple functions. A function to insert
10 min read
Insert a linked list into another linked list
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. Examples: Input: a = 3, b = 4head1: 10 -> 11 -> 12 -> 13 -> 14 -> 15, head2: 100 -> 101 -> 102 -> 103O
10 min read
First common element in two linked lists
Given two Linked Lists, find the first common element between given linked list i.e., we need to find first node of the first list which is also present in the second list. Examples: Input : List1: 10->15->4->20 Lsit2: 8->4->2->10 Output : 10 Input : List1: 1->2->3->4 Lsit
6 min read
In-place Merge two linked lists without changing links of first list
Given two sorted singly linked lists having n and m elements each, merge them using constant space. First n smallest elements in both the lists should become part of first list and rest elements should be part of second list. Sorted order should be maintained. We are not allowed to change pointers o
11 min read