// 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;
}
// 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;
}
const reverseList = function(head) {
let prev = null;
while (head !== null) {
let next = head.next;
head.next = prev;
prev = head
head = next;
}
return previous;
};