I am trying to solve palindrome linked list on leetcode with brute force, but I am stuck in a logical problem. What's the difference between a head node and a reference to a head node?
class Solution {
public boolean isPalindrome(ListNode head) {
ListNode prev = null;
ListNode current_head = head; // reference to head
while (head != null) // reversing a linked list
{
ListNode next_node = current_head.next; // here is the error
current_head.next = prev;
prev = current_head;
current_head = next_node;
}
while (prev != null && head != null) //trying to print values.
{
System.out.println("prev :" + prev.val);
System.out.println(" head :" + head.val);
head = head.next;
prev = prev.next;
}
/* while(prev != null && current_head !=null) //checking if it is a palindrome.
{
if(prev.val != current_head.val)
{
return false;
}
else{
prev=prev.next;
current_head= current_head.next;
}
}
*/
return true;
}
}
The error:
WARNING: A command line option has enabled the Security Manager
WARNING: The Security Manager is deprecated and will be removed in a future release
java.lang.NullPointerException: Cannot read field "next" because "<local3>" is null
at line 19, Solution.isPalindrome
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main