Advertisement
Toluvaa

Untitled

Apr 30th, 2024
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. class Solution {
  2.     public boolean isPalindrome(ListNode head) {
  3.         if(head==null || head.next==null)
  4.         {
  5.             return true;
  6.         }  
  7.         ListNode r=rev(head);
  8.         while(head!=null && r!=null)
  9.         {
  10.             if(head.val!=r.val)
  11.             {
  12.                 return false;
  13.             }
  14.             head=head.next;
  15.             r=r.next;
  16.         }
  17.         return true;
  18.     }
  19.     public ListNode rev(ListNode head)
  20.     {
  21.         if(head==null || head.next==null)
  22.         {
  23.             return head;
  24.         }
  25.         ListNode a=rev(head.next);
  26.         head.next.next=head;
  27.         head.next=null;
  28.  
  29.         return a;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement