Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given a linked list and an integer X, partition the LL around X, such that all nodes less than X come before all nodes greater than X. If X is contained within the list, then those nodes need
  3.  
  4. to be after the elements less than X and before the elements greater than X, i.e. they should appear between the left and right partitions.
  5.  
  6.  
  7. You can also see if you can preserve the order for elements on either side of the partition. For instance, for given LL 2, 6, 5, 7, 1 and X = 5, the answer should be 2, 1, 5, 6, 7 only, instead of 1, 2, 5, 6, 7.
  8.  
  9. Input format
  10. N - An integer denoting the number of nodes in the linked list.
  11.  
  12. N integers follow where ith integer denotes the ith node value in the linked list
  13.  
  14. X - An integer denoting the value that you must use to partition the given list
  15.  
  16. Output format
  17. Return the head after partitioning the list
  18.  
  19. Constraints
  20. 1 <= N <= 10^5
  21.  
  22. -10^9 <= value, X <= 10^9
  23.  
  24. Sample Input 1
  25. 7
  26.  
  27. 3 5 8 5 10 2 1
  28.  
  29. 5
  30.  
  31. Sample Output 1
  32. 3 2 1 5 5 8 10
  33.  
  34. Explanation 1
  35. The nodes [3], [1] and [2] are less than [5] so they are present before [8] and [10].
  36.  
  37. There are also other possible answers for the same input.
  38.  
  39. Sample Input 2
  40. 5
  41.  
  42. 3 1 3 1 4
  43.  
  44. 2
  45.  
  46. Sample Output 2
  47. 1 1 4 3 3
  48.  
  49. Explanation 2
  50. All nodes with values less than 2 come before all nodes with values greater than 2.
  51. */
  52.  
  53. /*
  54. class ListNode{
  55.     constructor(val){
  56.         this.val = val;
  57.         this.next = null;
  58.     }
  59. */
  60. /*
  61. In JavaScript, when you pass an object (like a ListNode or any other object) as an argument to a function, you are actually passing a reference to that object. However, there is a distinction between passing by value and passing by reference that is important to understand.
  62.  
  63. In the case of your insertNode function, you are passing newHead and newLastPointer as arguments. These arguments hold references to objects (ListNode instances) that exist outside of the function. When you modify the properties of the object that newHead and newLastPointer reference, those changes are reflected outside of the function.
  64.  
  65. However, when you reassign newHead and newLastPointer to new values inside the insertNode function, you are not modifying the original references that were passed as arguments. Instead, you are creating new local references within the function that point to different objects. These new references do not affect the original references outside of the function.
  66.  
  67. To update newHead and newLastPointer effectively, you can return the new values from the insertNode function and assign them outside of the function. Here's how you can modify the insertNode function:
  68. */
  69. function insertNode(newHead,newLastPointer,newVal){
  70.       if(newHead==null){
  71.             newHead=new ListNode(newVal);
  72.             newLastPointer=newHead;
  73.       }
  74.       else{
  75.             newLastPointer.next=new ListNode(newVal);
  76.             newLastPointer=newLastPointer.next;
  77.       }
  78.       return [newHead,newLastPointer];
  79. }
  80. /**
  81.  * @param {ListNode} head
  82.  * @param {number} X
  83.  * @return {ListNode}
  84.  */
  85. function partition(head, x) {
  86.      let newHead=null;
  87.      let currNode=head;
  88.      let newLastPointer=null;
  89.      // two iterations needed
  90.      while(currNode!=null){
  91.            if(currNode.val<x){
  92.                  [newHead, newLastPointer]=insertNode(newHead,newLastPointer,currNode.val);
  93.            }
  94.       //      console.log(newHead)
  95.            currNode=currNode.next;
  96.      }
  97.      // insertNode(newHead,newLastPointer,x);
  98.      currNode=head;
  99.      while(currNode!=null){
  100.            if(currNode.val==x){
  101.                 [newHead, newLastPointer]= insertNode(newHead,newLastPointer,currNode.val)
  102.            }
  103.            currNode=currNode.next
  104.      }
  105.  
  106.      currNode=head;
  107.      while(currNode!=null){
  108.            if(currNode.val>x){
  109.                  [newHead, newLastPointer]=insertNode(newHead,newLastPointer,currNode.val)
  110.            }
  111.            currNode=currNode.next
  112.      }
  113. //      console.log(newHead)
  114.      return newHead;
  115.  
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement