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 :: GET http://localhost:8000/js/app.js net::ERR_ABORTED 404 (Not Found) in laravel 6 
Javascript :: fontsize javascript 
Javascript :: exit foreach loop js 
Javascript :: checkbox event listeners 
Javascript :: ordenar un array de menor a mayor 
Javascript :: how to get table last row id in jquery 
Javascript :: max js 
Javascript :: async function js 
Javascript :: how to generate random color hexcodes for javascript 
Javascript :: call apply and bind method in javascript 
Javascript :: arrow functions in es6 
Javascript :: javascript sleep 1 second" 
Javascript :: get the value of an input nativscript 
Javascript :: Expected a JavaScript module script but the server responded with a MIME type of "text/html" 
Javascript :: save networkx graph to json 
Javascript :: text input placeholder font family react native 
Javascript :: javascript store value in array 
Javascript :: run react app 
Javascript :: how to allow implicit any in .d.ts 
Javascript :: cancel or abort axios request 
Javascript :: Connect to socket.io node.js command line 
Javascript :: how to setup icomoon in react js 
Javascript :: how to update node in terminal 
Javascript :: make multiple api call using promise.all 
Javascript :: convert curl response to json format and echo the data 
Javascript :: javascript if statement 
Javascript :: axios post request javascript 
Javascript :: javascript string proper case 
Javascript :: invoking jquery validator 
Javascript :: numberformat react phone number 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =