knakul853

Untitled

Jul 16th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
  7.        
  8.         ListNode* root = new ListNode();
  9.         ListNode*temp = root;
  10.        
  11.         while(l1 && l2)
  12.         {
  13.             if(l1->val > l2->val)
  14.             {
  15.                 root->next = new ListNode(l2->val);
  16.                 root=root->next;
  17.                 l2 = l2->next;
  18.             }
  19.             else
  20.             {
  21.                
  22.                 root->next = new ListNode(l1->val);
  23.                 l1 = l1->next;
  24.                 root=root->next;
  25.             }
  26.         }
  27.        
  28.        root->next = l1 ? l1 : l2;
  29.        
  30.       return  temp->next;
  31.        
  32.     }
  33. };
Add Comment
Please, Sign In to add comment