You can see the problem in the attached picture. I also tried the print statement, "System.out.println(palindrome.val);" and I'm getting the same error. Please help me fix this. Thanks
Asked
Active
Viewed 39 times
1 Answers
0
In the reverse
function you are looping while head
is not equals to null
. After the loop you are returning head
, which will definitely be null when the loop breaks.
So palindrome
is null
every time. And so doing palindrome.val
gives NullPointerException
.
To avoid this you can simple return the prev
node from the reverse function.

Robert Dean
- 685
- 5
- 16

vishal
- 157
- 1
- 10