public class IsHuiWenUpdate2 {
public static boolean isHuiwenUpdate(Node head)
{
Node n1=head;
Node n2=head;
while(n2.next!=null&&n2.next.next!=null)
{
n1=n1.next;
n2=n2.next.next;
}
n2=n1.next;
Node pre=n1;
Node next=null;
while(n2!=null)
{
next=n2.next;
n2.next=pre;
pre=n2;
n2=next;
}
n1.next=null;
n2=pre;
n1=head;
while(n1.next!=null&&n2.next!=null)
{
if(n1.data!=n2.data)
return false;
n1=n1.next;
n2=n2.next;
}
next=null;
n2=null;
while(pre!=null)
{
next=pre.next;
pre.next=n2;
n2=pre;
pre=next;
}
return true;
}
}
public class Test {
public static void main(String[] args) {
CreateQueue cq=new CreateQueue();
boolean b=IsHuiWenUpdate2.isHuiwenUpdate(cq.a);
System.out.println("链表是回文吗?"+b);
}
}
public class CreateQueue {
Node a=new Node(1);
Node b=new Node(2);
Node c=new Node(3);
Node d=new Node(4);
Node e=new Node(3);
Node f=new Node(2);
Node g=new Node(1);
public CreateQueue()
{
a.next=b;
b.next=c;
c.next=d;
d.next=e;
e.next=f;
f.next=g;
g.next=null;
}
}