Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given 2 sorted linked lists, merge them into a new sorted linked list and return head of the merged list. The new list should be made by splicing (adjusting the pointers) together the nodes of the first two lists.
  3.  
  4. Input format
  5. N - An integer denoting the number of nodes in the linked list.
  6.  
  7. N integers follow where ith integer denotes the ith node value in the linked list
  8.  
  9. M - An integer denoting the number of nodes in the linked list.
  10.  
  11. M integers follow where jth integer denotes the jth node value in the linked list
  12.  
  13. Output format
  14. Return the sorted list after splicing the nodes of the first two lists.
  15.  
  16. Constraints
  17. 0 <= N <= 10^5
  18.  
  19. -10^9 <= value <= 10^9
  20.  
  21. 0 <= M <= 10^5
  22.  
  23. -10^9 <= value <= 10^9
  24.  
  25. Sample Input 1
  26. 3
  27.  
  28. 1 2 4
  29.  
  30. 3
  31.  
  32. 1 3 4
  33.  
  34. Sample Output 1
  35. 1 1 2 3 4 4
  36.  
  37. Explanation 1
  38. Merging the input lists and keeping the new list sorted results in this.
  39.  
  40. Sample Input 2
  41. 4
  42.  
  43. 1 5 7 6
  44.  
  45. 1
  46.  
  47. 3
  48.  
  49. Sample Output 2
  50. 1 3 5 7 6
  51.  
  52. Explanation 2
  53. Merging the input lists and keeping the new list sorted results in this.
  54. */
  55.  
  56. /*
  57. class ListNode{
  58.     constructor(val){
  59.         this.val = val;
  60.         this.next = null;
  61.     }
  62. */
  63. /**
  64.  * @param {ListNode} l1
  65.  * @param {ListNode} l2
  66.  * @return {ListNode}
  67.  */
  68. function mergeTwoLists(l1, l2) {
  69.       let pointer1=l1,pointer2=l2;
  70.       let head=pointer1;
  71.       // pointer1 always poiints to list with smaller value of its first node;
  72.       // now appoerach is i will take pointer1 as parent list and insert other values into this parent list
  73.       if(pointer2==null)
  74.             return pointer1;
  75.       if(pointer1==null)
  76.             return pointer2;
  77.       if(pointer1.val>pointer2.val){
  78.             head=pointer2;
  79.             [pointer1,pointer2]=[pointer2,pointer1];
  80.       }
  81.       // console.log(head)
  82.      
  83.       while(pointer1.next!=null&&pointer2!=null){
  84.             if(pointer1.next!=null&&pointer1.next.val<pointer2.val){
  85.                   pointer1=pointer1.next;
  86.             }
  87.             else{
  88.                   let tmp1 = pointer1.next;
  89.                   pointer1.next=pointer2;
  90.                   let tmp2 = pointer2.next;
  91.                   pointer2.next = tmp1;
  92.                   // move one step forward
  93.                   pointer1 = pointer1.next;
  94.                   pointer2 = tmp2;
  95.             }
  96.       }
  97.  
  98.       if(pointer2==null){
  99.             return head;
  100.       }
  101.       else{
  102.             // pointer1.next===null
  103.             pointer1.next=pointer2;
  104.       }
  105.       return head;
  106.  
  107.  
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement