Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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;
}
Source by wsvincent.com #
 
PREVIOUS NEXT
Tagged: #js #reverse #linked #list
ADD COMMENT
Topic
Name
4+2 =