Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given a singly linked list of integers, reverse every contiguous set of nodes that have only even values.
  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. Output format
  10. Return the head of the modified list
  11.  
  12. Constraints
  13. 0 <= N <= 10^5
  14.  
  15. -10^9 <= value <= 10^9
  16.  
  17. Sample Input 1
  18. 8
  19.  
  20. 1 2 3 3 4 6 8 5
  21.  
  22. Sample Output 1
  23. 1 2 3 3 8 6 4 5
  24.  
  25. Explanation 1
  26. There are two sublists of even elements, which [2] and [4->6->8]. The sublist [4->6->8] has been reversed and the single sublist [2] need not be reversed.
  27.  
  28. Sample Input 2
  29. 6
  30.  
  31. 1 3 2 5 4 6
  32.  
  33. Sample Output 2
  34. 1 3 2 5 6 4
  35.  
  36. Explanation 2
  37. There are two sublists of even elements which are [2] and [4 6]. The [4,6] sublist has been reversed and the single sublist [2] need not be reversed. Rest of the odd values remain constant.
  38.  
  39.  
  40.  
  41.  
  42. */
  43.  
  44.  
  45. /*
  46. class ListNode{
  47.     constructor(val){
  48.         this.val = val;
  49.         this.next = null;
  50.     }
  51. */
  52.  
  53. /**
  54.  * @param {ListNode} head
  55.  * @return {ListNode}
  56.  */
  57. function reverseEvenElements(head) {
  58.       let pointer=head, arr=[], resultantArr=[],j=0;
  59.       while(pointer!=null){
  60.             let subarray=[];
  61.             let currVal=pointer.val;
  62.             if(currVal%2!=0){
  63.                   subarray.push(currVal);
  64.                   arr.push(subarray);
  65.                   pointer=pointer.next;
  66.             }else{
  67.                   subarray.push(currVal);
  68.                   pointer=pointer.next;
  69.                   while(pointer!=null){
  70.                        let currVal=pointer.val;
  71.                        if(currVal%2==0){
  72.                              subarray.push(currVal);
  73.                              pointer=pointer.next;
  74.                        }else{
  75.                              arr.push(subarray);
  76.                              break;
  77.                        }
  78.                   }
  79.                   if(pointer==null){
  80.                         arr.push(subarray);
  81.                   }
  82.             }
  83.  
  84.       }
  85.  
  86.       // console.log(arr);
  87.       for(let i=0;i<arr.length;i++){
  88.             if(arr[i].length>1){
  89.                 arr[i]=arr[i].reverse();
  90.             //     console.log("even subarray")
  91.             //     console.log(arr[i]);
  92.             }
  93.             resultantArr=resultantArr.concat(arr[i]);
  94.       }
  95.       // console.log(resultantArr);
  96.       pointer=head;
  97.  
  98.       while(pointer!=null){
  99.             pointer.val=resultantArr[j];
  100.             j++;
  101.             pointer=pointer.next;
  102.       }
  103.  
  104.       return head;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement