gur111

Ital PVT 28.03.2019 isPalindrome IntList

Mar 28th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. public class IntList {
  2.     private IntNode _head;
  3.    
  4.     public IntList( ) {
  5.         _head = null;
  6.     }
  7.     public IntList (IntNode node) {
  8.         _head = node;
  9.     }
  10.    
  11.     public static boolean isPalindrome(IntNode n){
  12.         int count, half;
  13.         IntNode head1 = n, tail, mid, head2;
  14.        
  15.         /* code counting here. */
  16.        
  17.         half = count/2;
  18.        
  19.         mid = head1;
  20.        
  21.         for(int i=0; i<half+count%2; i++){
  22.             mid = mid.getNext();
  23.         }
  24.        
  25.         tail = reverseList(mid, half-1);
  26.         head2 = tail;
  27.         for(int i = 0; i<half; i++){
  28.             if(head2.getValue() != n.getValue()){
  29.                 reverseList(tail, half-1);
  30.                 return false;
  31.             }
  32.         }
  33.         reverseList(tail, half-1);
  34.         return true;
  35.        
  36.     }
  37.    
  38.     public static IntNode reverseList(IntNode n, int count){
  39.         IntNode prev = n, cur = n.getNext(), next;
  40.         for(int i = 1; i<count; i++){
  41.             next = cur.getNext();
  42.             cur.setNext(prev);
  43.             prev = cur;
  44.             cur = next;
  45.         }
  46.         return prev;
  47.     }
  48.    
  49. }
Add Comment
Please, Sign In to add comment