const data = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: {
value: 5,
next: null,
},
},
},
},
};
 
 
 
function reverse(head) {
let currNext = null
let currObj = head
 
while (currObj !== null) {
let nextNode = currObj.next
currObj.next = currNext
currNext = currObj
currObj = nextNode
}
 
return currNext
}
 
 
console.log(reverse(data))
Click Run to execute JavaScript.
Code runs in an isolated environment.