lamiastella

isPalindrome

Mar 12th, 2016
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. public static LLNode reverseList(LLNode head){
  2.         if (head == null){
  3.             return null;
  4.         }
  5.  
  6.         if (head.next == null){
  7.             return head;
  8.         }
  9.  
  10.  
  11.         LLNode headNext = head.next;
  12.  
  13.         head.next = null;
  14.  
  15.         LLNode reverseHeadNext = reverseList(headNext);
  16.  
  17.  
  18.         headNext.next = head;
  19.  
  20.         return reverseHeadNext;
  21.     }
  22.  
  23.  
  24.  public static boolean isPalindrome(LLNode head) {
  25.         LLNode headCopy=head;
  26.         LLNode reverseHead=reverseList(headCopy);
  27.         LLNode tmp1=head;
  28.         LLNode tmp2=reverseHead;
  29.          while (tmp1.next!=null){
  30.             if (tmp1.data!=tmp2.data){
  31.                 return false;
  32.             }
  33.             else{
  34.                 tmp1=tmp1.next;
  35.                 tmp2=tmp2.next;
  36.             }
  37.         }
  38.         if (tmp1!=tmp2){
  39.             return false;
  40.         }
  41.         return true;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment