/**
- Definition for singly-linked list.
- public class ListNode {
-
int val; -
ListNode next; -
ListNode() {} -
ListNode(int val) { this.val = val; } -
ListNode(int val, ListNode next) { this.val = val; this.next = next; } - }
*/
class Solution {
private ListNode reverseList(ListNode head) {
if(head==null || head.next==null)
return head;
ListNode prev=head;
ListNode cur=prev.next;
ListNode n= cur.next;
while(cur.next!=null)
{
cur.next=prev;
prev=cur;
cur=n;
n=n.next;
}
cur.next=prev;
head.next=null;
head=cur;
return head;
}
public boolean isPalindrome(ListNode head) {
if(head==null || head.next==null)
return true;
ListNode head1=head;
ListNode rev=reverseList(head);
while(head1!=null)
{
if(head1.val==rev.val)
{
head1=head1.next;
rev=rev.next;
}
else
return false;
}
return true;
}
}
this is the code and I’m getting wrong answer for list [1,1,2,1] but reverse function gives right answer for that list. what is wrong?