Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given the elements of a linked list, reverse it.
  3.  
  4.  
  5. You’ll have to implement the given method, which has the original list’s head pointer as an argument, and return the head of the updated list.
  6.  
  7. Input format
  8. There are two lines of input.
  9.  
  10. First line contains N, the number of elements in the linked list.
  11.  
  12. Second line contains N space separated integers.
  13.  
  14. Output format
  15. Only line contains N space separated integers
  16.  
  17. Sample Input 1
  18. 5
  19.  
  20. 1 2 3 4 5
  21.  
  22. Sample Output 1
  23. 5 4 3 2 1
  24.  
  25. Explanation 1
  26. 1->2->3->4->5->NULL
  27.  
  28. 5->4->3->2->1->NULL
  29.  
  30. Constraints
  31. 0 <= N <= 10^5
  32.  
  33. -10^9 <= A[i] <= 10^9
  34. */
  35.  
  36. /*
  37. class ListNode{
  38.     constructor(val){
  39.         this.val = val;
  40.         this.next = null;
  41.     }
  42. */
  43. /**
  44.  * @param {ListNode} head
  45.  * @return {ListNode}
  46.  */
  47. function reverseLinkedList(head) {
  48.       let prevNode=null;
  49.       let currNode=head;
  50.       while(currNode!=null){
  51.             let next=currNode.next;
  52.             currNode.next=prevNode;
  53.             prevNode=currNode;
  54.             currNode=next;
  55.       }
  56.     return prevNode;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement