gur111 icon

Ital PVT 28.03.2019 isPalindrome IntList

gur111 | PRO | 03/28/19 09:13:52 PM UTC | 0 ⭐ | 433 👁️ | Never ⏰ | []
Java |

890 B

|

None

|

0 👍

/

0 👎

public class IntList {
    private IntNode _head;
    
    public IntList( ) {
        _head = null;
    }
    public IntList (IntNode node) {
        _head = node;
    }
    
    public static boolean isPalindrome(IntNode n){
        int count, half;
        IntNode head1 = n, tail, mid, head2;
        
        /* code counting here. */
        
        half = count/2;
        
        mid = head1;
        
        for(int i=0; i<half+count%2; i++){
            mid = mid.getNext();
        }
        
        tail = reverseList(mid, half-1);
        head2 = tail;
        for(int i = 0; i<half; i++){
            if(head2.getValue() != n.getValue()){
                reverseList(tail, half-1);
                return false;
            }
        }
        reverseList(tail, half-1);
        return true;
        
    }
    
    public static IntNode reverseList(IntNode n, int count){
        IntNode prev = n, cur = n.getNext(), next;
        for(int i = 1; i<count; i++){
            next = cur.getNext();
            cur.setNext(prev);
            prev = cur;
            cur = next;
        }
        return prev;
    }
    
}

Comments