Create new linked list from two given linked list with greater element at each node - GeeksforGeeks

28 Mar, 2022
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++
Java
Python3
C#
Javascript
// 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 nodestruct Node {    int data;    Node* next;}; // Function to insert node in a linked listvoid 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 listNode* 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 codeint 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 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 nodeimport java.util.*; class GFG{ // Representation of nodestatic class Node{    int data;    Node next;}; // Function to insert node in a linked liststatic 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 liststatic 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 codepublic 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
# 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 classclass Node:     # Function to initialise the node object    def __init__(self, data):        self.data = data        self.next = None # Function to insert node in a linked listdef 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 listdef 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 Codeif __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# 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 nodeusing System;     class GFG{ // Representation of nodepublic class Node{    public int data;    public Node next;}; // Function to insert node in a linked liststatic 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 liststatic 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 codepublic 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
<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 nodeclass 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>
First List:  5->2->3->8->
Second List: 1->7->4->5->
New List:    5->7->4->8->
 
andrew1234
29AjayKumar
GauravRajput1
varshagumber28
Traversal
Linked List
Mathematical
Linked List
Mathematical
Traversal
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Doubly Linked List | Set 1 (Introduction and Insertion)
Linked List vs Array
Merge two sorted linked lists
Implementing a Linked List in Java using Class
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7