const n1 = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
}
function reverse1(root) {
if(root.next === null) {
return root
}
const last = reverse(root.next);
root.next.next = root;
root.next = null;
return last;
}
function reverse2(root) {
let pre = null;
let curr = root;
while(curr !== null) {
const temp = curr.next;
curr.next = pre;
pre = curr;
curr = temp;
}
return pre;
}
const res = reverse2(n1);
console.log(res)