Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

reverse a linked list javascript

// O(n) time & O(n) space
function reverse(head) {
  if (!head || !head.next) {
    return head;
  }
  let tmp = reverse(head.next);
  head.next.next = head;
  head.next = undefined;
  return tmp;
}
Comment

js reverse linked list

// non recussive
function reverse(head) {
  let node = head,
      previous,
      tmp;

  while (node) {
    // save next before we overwrite node.next!
    tmp = node.next;

    // reverse pointer
    node.next = previous;

    // step forward in the list
    previous = node;
    node = tmp;
  }

  return previous;
}
Comment

invert linked list js

const reverseList = function(head) {
    let prev = null;
    while (head !== null) {
        let next = head.next;
        head.next = prev;
        prev = head
        head = next;
    }
    return previous;
};
Comment

206. Reverse Linked List javascript

var reverseList = function(head) {
    let prev = null;
    let curr = head;
    let nextTemp = null;

    while(curr!= null) {
        nextTemp = curr.next; // As I explained earlier, I save the next pointer in the temp variable.
        curr.next = prev;  // Then I reverse the pointer of the current node to its previous node.
        prev = curr;  //  The previous node becomes the node we are currently at.
        curr = nextTemp;  // And the current nodes becomes the
next node we saved earlier. And we keep iterating.
    }
    return prev // At the end, our previous node will be the head node of the new list. 
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: js submit 
Javascript :: how to test usestate in jest 
Javascript :: convert timestamp to date javascript 
Javascript :: get current date + 1 js 
Javascript :: difference between type and method in ajax 
Javascript :: setinterval vs settimeout js 
Javascript :: javascript change div order 
Javascript :: trigger send parameter 
Javascript :: find class using jquery 
Javascript :: canvas umu 
Javascript :: $(document).ready, window.onload 
Javascript :: read csv file in javascript 
Javascript :: how to send static file in express 
Javascript :: how to master javascript 
Javascript :: convert map to object/JSON javascript 
Javascript :: mongoose find and update prop 
Javascript :: You need to authorize this machine using `npm adduser` 
Javascript :: javascript on image load 
Javascript :: how to append rows in table using jquery each function 
Javascript :: remove a specific element from an array 
Javascript :: set time to zero in js date 
Javascript :: tab active check 
Javascript :: word count javascript 
Javascript :: react onclick with event 
Javascript :: org.json.JSONException: End of input at character 0 of 
Javascript :: destructure dynamic property 
Javascript :: js make node with string 
Javascript :: multidimensional array push in jquery 
Javascript :: ... unicode 
Javascript :: how to change text of div in javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =