Create new linked list from two given linked list with greater element at each node | 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 ; Representation of node ; Function to insert node in a linked list ; Function which returns new linked list ; Compare for greater node ; Driver code ; First linked list ; Second linked list
#include <iostream> NEW_LINE 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 ; } } 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 ; 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 ; } int main ( ) { Node * root1 = NULL , * root2 = NULL , * root = NULL ; insert ( & root1 , 5 ) ; insert ( & root1 , 2 ) ; insert ( & root1 , 3 ) ; insert ( & root1 , 8 ) ; cout << " First ▁ List : ▁ " ; display ( root1 ) ; insert ( & root2 , 1 ) ; insert ( & root2 , 7 ) ; insert ( & root2 , 4 ) ; insert ( & root2 , 5 ) ; cout << " Second ▁ List : ▁ " ; display ( root2 ) ; root = newList ( root1 , root2 ) ; cout << " New ▁ List : TABSYMBOL " ; display ( root ) ; return 0 ; }