Create new linked list from two given linked list with greater element at each node
Last Updated :
24 Jun, 2024
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->8
list2 = 1->7->4->5
Output: New list = 5->7->4->8
Input: list1 = 2->8->9->3
list2 = 5->3->6->4
Output: New list = 5->8->9->4
Approach: We traverse both the linked list at the same time and compare node of both lists. The node which is greater among them will be added to the new linked list. We do this for each node.
C++
// C++ program to create a new linked list
// from two given linked list
// of the same size with
// the greater element among the two at each node
#include <iostream>
using namespace std;
// Representation of node
struct Node {
int data;
Node* next;
};
// Function to insert node in a linked list
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;
}
}
// Function which returns new linked list
Node* newList(Node* root1, Node* root2)
{
Node *ptr1 = root1, *ptr2 = root2, *ptr;
Node *root = NULL, *temp;
while (ptr1 != NULL) {
temp = new Node;
temp->next = NULL;
// Compare for greater node
if (ptr1->data < ptr2->data)
temp->data = ptr2->data;
else
temp->data = ptr1->data;
if (root == NULL)
root = temp;
else {
ptr = root;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return root;
}
void display(Node* root)
{
while (root != NULL) {
cout << root->data << "->";
root = root->next;
}
cout << endl;
}
// Driver code
int main()
{
Node *root1 = NULL, *root2 = NULL, *root = NULL;
// First linked list
insert(&root1, 5);
insert(&root1, 2);
insert(&root1, 3);
insert(&root1, 8);
cout << "First List: ";
display(root1);
// Second linked list
insert(&root2, 1);
insert(&root2, 7);
insert(&root2, 4);
insert(&root2, 5);
cout << "Second List: ";
display(root2);
root = newList(root1, root2);
cout << "New List: ";
display(root);
return 0;
}
Java
// Java program to create a new linked list
// from two given linked list
// of the same size with
// the greater element among the two at each node
import java.util.*;
class GFG
{
// Representation of node
static class Node
{
int data;
Node next;
};
// Function to insert node in a linked list
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;
}
// Function which returns new linked list
static Node newList(Node root1, Node root2)
{
Node ptr1 = root1, ptr2 = root2, ptr;
Node root = null, temp;
while (ptr1 != null) {
temp = new Node();
temp.next = null;
// Compare for greater node
if (ptr1.data < ptr2.data)
temp.data = ptr2.data;
else
temp.data = ptr1.data;
if (root == null)
root = temp;
else {
ptr = root;
while (ptr.next != null)
ptr = ptr.next;
ptr.next = temp;
}
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return root;
}
static void display(Node root)
{
while (root != null)
{
System.out.print( root.data + "->");
root = root.next;
}
System.out.println();
}
// Driver code
public static void main(String args[])
{
Node root1 = null, root2 = null, root = null;
// First linked list
root1=insert(root1, 5);
root1=insert(root1, 2);
root1=insert(root1, 3);
root1=insert(root1, 8);
System.out.print("First List: ");
display(root1);
// Second linked list
root2=insert(root2, 1);
root2=insert(root2, 7);
root2=insert(root2, 4);
root2=insert(root2, 5);
System.out.print( "Second List: ");
display(root2);
root = newList(root1, root2);
System.out.print("New List: ");
display(root);
}
}
// This code is contributed by Arnab Kundu
Python
# Python3 program to create a
# new linked list from two given
# linked list of the same size with
# the greater element among the two
# at each node
# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data
self.next = None
# Function to insert node in a linked list
def insert(root, item):
temp = Node(0)
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
# Function which returns new linked list
def newList(root1, root2):
ptr1 = root1
ptr2 = root2
root = None
while (ptr1 != None) :
temp = Node(0)
temp.next = None
# Compare for greater node
if (ptr1.data < ptr2.data):
temp.data = ptr2.data
else:
temp.data = ptr1.data
if (root == None):
root = temp
else :
ptr = root
while (ptr.next != None):
ptr = ptr.next
ptr.next = temp
ptr1 = ptr1.next
ptr2 = ptr2.next
return root
def display(root):
while (root != None) :
print(root.data, "->", end = " ")
root = root.next
print(" ");
# Driver Code
if __name__=='__main__':
root1 = None
root2 = None
root = None
# First linked list
root1 = insert(root1, 5)
root1 = insert(root1, 2)
root1 = insert(root1, 3)
root1 = insert(root1, 8)
print("First List: ", end = " ")
display(root1)
# Second linked list
root2 = insert(root2, 1)
root2 = insert(root2, 7)
root2 = insert(root2, 4)
root2 = insert(root2, 5)
print("Second List: ", end = " ")
display(root2)
root = newList(root1, root2)
print("New List: ", end = " ")
display(root)
# This code is contributed by Arnab Kundu
C#
// C# program to create a new linked list
// from two given linked list
// of the same size with
// the greater element among the two at each node
using System;
class GFG
{
// Representation of node
public class Node
{
public int data;
public Node next;
};
// Function to insert node in a linked list
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;
}
// Function which returns new linked list
static Node newList(Node root1, Node root2)
{
Node ptr1 = root1, ptr2 = root2, ptr;
Node root = null, temp;
while (ptr1 != null)
{
temp = new Node();
temp.next = null;
// Compare for greater node
if (ptr1.data < ptr2.data)
temp.data = ptr2.data;
else
temp.data = ptr1.data;
if (root == null)
root = temp;
else
{
ptr = root;
while (ptr.next != null)
ptr = ptr.next;
ptr.next = temp;
}
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return root;
}
static void display(Node root)
{
while (root != null)
{
Console.Write( root.data + "->");
root = root.next;
}
Console.WriteLine();
}
// Driver code
public static void Main(String []args)
{
Node root1 = null, root2 = null, root = null;
// First linked list
root1 = insert(root1, 5);
root1 = insert(root1, 2);
root1 = insert(root1, 3);
root1 = insert(root1, 8);
Console.Write("First List: ");
display(root1);
// Second linked list
root2 = insert(root2, 1);
root2 = insert(root2, 7);
root2 = insert(root2, 4);
root2 = insert(root2, 5);
Console.Write( "Second List: ");
display(root2);
root = newList(root1, root2);
Console.Write("New List: ");
display(root);
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// javascript program to create a new linked list
// from two given linked list
// of the same size with
// the greater element among the two at each node
// Representation of node
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
// Function to insert node in a linked list
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 which returns new linked list
function newList( root1, root2) {
var ptr1 = root1, ptr2 = root2, ptr;
var root = null, temp;
while (ptr1 != null) {
temp = new Node();
temp.next = null;
// Compare for greater node
if (ptr1.data < ptr2.data)
temp.data = ptr2.data;
else
temp.data = ptr1.data;
if (root == null)
root = temp;
else {
ptr = root;
while (ptr.next != null)
ptr = ptr.next;
ptr.next = temp;
}
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
return root;
}
function display( root) {
while (root != null) {
document.write(root.data + "->");
root = root.next;
}
document.write("<br/>");
}
// Driver code
root1 = null, root2 = null, root = null;
// First linked list
root1 = insert(root1, 5);
root1 = insert(root1, 2);
root1 = insert(root1, 3);
root1 = insert(root1, 8);
document.write("First List: ");
display(root1);
// Second linked list
root2 = insert(root2, 1);
root2 = insert(root2, 7);
root2 = insert(root2, 4);
root2 = insert(root2, 5);
document.write("Second List: ");
display(root2);
root = newList(root1, root2);
document.write("New List: ");
display(root);
// This code is contributed by gauravrajput1
</script>
OutputFirst List: 5->2->3->8->
Second List: 1->7->4->5->
New List: 5->7->4->8->
Time complexity: O(n) where n is size of linked list
Space complexity: O(1) since using constant space
Similar Reads
Next Greater Element in a Circular Linked List Given a circular singly linked list, the task is to print the next greater element for each node in the linked list. If there is no next greater element for any node, then print "-1" for that node. Examples: Input: head = 1 ? 5 ? 2 ? 10 ? 0 ? (head)Output: 5 10 10 -1 -1Explanation:The next greater e
11 min read
Next greater element in the Linked List Given a linked list L of integers, the task is to return a linked list of integers such that it contains next greater element for each element in the given linked list. If there doesn't any greater element for any element then insert 0 for it. Examples: Input: 2->1->3->0->5 Output: 3-
15+ 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
Delete all the nodes from the doubly linked list that are greater than a given value Given a doubly linked list containing N nodes and a number X, the task is to delete all the nodes from the list that are greater than the given value X. Examples: Input: 10 8 4 11 9, X = 9 Output: 8 4 9 Explanation: 10 and 11 are greater than 9. So after removing them doubly linked list will become
11 min read
Find the Second Largest Element in a Linked List Given a Linked list of integer data. The task is to write a program that efficiently finds the second largest element present in the Linked List. Examples: Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1 Output : The second largest element is 34. Input : List = 10 -> 5 -> 10 Outpu
9 min read
Delete linked list nodes which have a greater value on left side Given a singly linked list, the task is to remove all the nodes which have a greater value on the left side. Examples: Input: 12->15->10->11->5->6->2->3Output: Modified Linked List = 12 15 Input: 25->15->6->48->12->5->16->14Output: Modified Linked List = 14
7 min read
Insert a Node before a given node in Doubly Linked List Given a Doubly Linked List, the task is to insert a new node before a given node in the linked list.Examples: Input: Linked List = 1 <-> 3 <-> 4, newData = 2, key = 3Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node with data 2 is inserted before the node w
12 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 Node after a given node in Doubly Linked List Given a Doubly Linked List, the task is to insert a new node after a given node in the linked list.Examples: Input: Linked List = 1 <-> 2 <-> 4, newData = 3, key = 2Output: Linked List = 1 <-> 2 <-> 3 <-> 4Explanation: New node 3 is inserted after key, that is node 2.In
11 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 -> 103Out
10 min read