Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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;
}
Source by wsvincent.com #
 
PREVIOUS NEXT
Tagged: #reverse #linked #list #javascript
ADD COMMENT
Topic
Name
1+1 =