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

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 :: nodejs convert string to date 
Javascript :: call bind apply in javascript 
Javascript :: js replace last occurrence of string 
Javascript :: button prevent default 
Javascript :: singleton class in js 
Javascript :: nextjs apollo client 
Javascript :: javascript buffer to file 
Javascript :: req.body showing undefined 
Javascript :: flatpickr current date set to text field 
Javascript :: larevel blade data received in javascript 
Javascript :: user authentication and routing + nodejs + express 
Javascript :: vscode module path aliases 
Javascript :: how dynamique pseudo element in react 
Javascript :: JavaScript for loop Display Numbers from 1 to 5 
Javascript :: javascript Access String Characters 
Javascript :: matrix calculator in js 
Javascript :: javascript this Inside Inner Function 
Javascript :: JavaScript HTML DOM Collections 
Javascript :: jsonformat iso 8601 
Javascript :: circular object array 
Javascript :: theme ui with react 17 
Javascript :: vuejs.org español 
Javascript :: phaser enable pixel art 
Javascript :: unicons add all icons 
Javascript :: file size to string js 
Javascript :: javascript to jquery code converter online 
Javascript :: js if animation infinity end 
Javascript :: javascript check item is checkbox 
Javascript :: are you sure alert js 
Javascript :: array objects 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =