class Node {
public int data;
public Node next;
public Node ( int data) {
this . data = data;
}
}
public class SingleList {
public Node head;
public void addFirst ( int data) {
Node node = new Node ( data) ;
if ( this . head == null) {
this . head = node;
return ;
} else {
node. next = this . head;
this . head = node;
}
}
public void show ( ) {
Node cur= this . head;
while ( cur!= null) {
System. out. print ( cur. data+ " " ) ;
cur= cur. next;
}
System. out. println ( ) ;
}
public boolean chkPalindrome ( ) {
if ( this . head== null) {
return false ;
}
if ( this . head. next== null) {
return true ;
}
Node fast = this . head;
Node slow = this . head;
while ( fast != null && fast. next != null) {
fast = fast. next. next;
slow = slow. next;
}
Node cur = slow. next;
while ( cur!= null) {
Node curNext= cur. next;
cur. next= slow;
slow= cur;
cur= curNext;
}
while ( this . head!= slow) {
if ( this . head. data!= slow. data) {
return false ;
}
if ( this . head. next== slow) {
return true ;
}
this . head= this . head. next;
slow= slow. next;
}
return true ;
}
}
public class TestDemo {
public static void main ( String[ ] args) {
SingleList singleList= new SingleList ( ) ;
singleList. addFirst ( 2 ) ;
singleList. addFirst ( 5 ) ;
singleList. addFirst ( 4 ) ;
singleList. addFirst ( 5 ) ;
singleList. addFirst ( 2 ) ;
singleList. show ( ) ;
System. out. println ( singleList. chkPalindrome ( ) ) ;
}
}