I know this problem is very well discussed in StackOverflow’s other posts, and I am here unable to figure out why I am getting this output.
This is the problem:
[206. Reverse Linked List][1]
This is my solution
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let lastOne = head
if(!(lastOne && lastOne.next)){
return head
}
head.next = null
let newNode = lastOne.next
const reverse = (node)=>{
if(node == null){
return lastOne
}
let newNode = node.next
node.next = lastOne
lastOne = node
return reverse(newNode)
}
return reverse(newNode)
};
I am getting this output:
[![enter image description here][2]][2]
head =
[1,2,3,4,5]
Output
[1]
Expected
[5,4,3,2,1]
can anyone help me, please !!