回文串为首尾对称的字符串:
如a,aba,abba等
1.将字符读入链表
2.找到链表中点
3.将链表从中点断开成2条,将后半条反转
4.比较两条链表是否相等(比较次数以少的为准(长度为奇数时))
//解法1.数组存储判断一个链表是否为回文链
public class TestHuiWen {
static class MyNode{
public int data;
public MyNode next;
public MyNode(int data,MyNode next){
this.data = data;
this.next = next;
}
}
public static void main(String[] args) {
// MyNode myNode6 = new MyNode(9,null);
MyNode myNode6 = new MyNode(1,null);
MyNode myNode5 = new MyNode(2,myNode6);
MyNode myNode3 = new MyNode(3,myNode5);
MyNode myNode2 = new MyNode(2,myNode3);
MyNode myNode1 = new MyNode(1,myNode2);
//
// while (myNode1!=null){
// int a = myNode1.data;
// System.out.println(a);
// myNode1 = myNode1.next;
// }
boolean flg = huiWen(myNode1);
System.out.println(flg);
}
public static boolean huiWen(MyNode head){
if(head == null || head.next == null){
return true;
}
int len = 0;
MyNode temp = head;
while (head!=null){
len++;
head = head.next;
}
int[] a = new int[len];
for(int i=0;i