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