您的位置:首页 > 其它

判断一个链表是否为回文结构

2017-10-31 16:54 399 查看


import java.util.Stack;

//判断一个链表是否为回文结构
public class HuiWenList{

//定义链表的节点
public static class Node{

int value;
Node next;

public Node(int data)
{
this.value=data;
}
}

//解法1.数组存储判断一个链表是否为回文链表
public static boolean isHuiWenList(Node head)
{

if(head==null||head.next==null)
{
return true;
}

int leng=0; //记录链表的长度
Node p=head;
while(p!=null)
{
++leng;
p=p.next;
}
int[]a=new int[leng]; //把链表的值存到数组中
for(int i=0;i<leng&&head!=null;i++)
{

a[i]=head.value;
head=head.next;
}

for(int j=0;j<(leng)/2;j++)
{
if(a[j]!=a[leng-j-1])
{
return false;
}
}
return true;

}
//解法2.栈存储判断一个链表是否为回文链表
public static boolean isHuiWenList2(Node head){
if(head==null||head.next==null)
{
return true;
}

Stack<Node> stack=new Stack<Node>(); //将所有的节点压入一个栈
Node p=head; //指向头结点
while(p!=null)
{
stack.push(p);
p=p.next;
}
while(head!=null)
{
if(head.value!=stack.pop().value)
{
return false;
}
head=head.next;

}
return true;

}

//解法3.栈存储(链表的一半)判断一个链表是否为回文链表
public static boolean isHuiWenList3(Node head){

if(head==null||head.next==null)
{
return true;
}
Stack <Integer> stack= new Stack<Integer>(); //存储链表节点的值(存储了链表节点数的一半)

Node right=head.next; //指向头的下一个节点
Node cur=head; //指向头节点

//获得右半部分以right开头
while(cur.next!=null&&cur.next.next!=null)
{
right=right.next;
cur=cur.next.next;
}
while(right!=null)
{
stack.push(right.value); //存储了链表节点数的一半
right=right.next;
}

while(!stack.isEmpty())
{
if(head.value!=stack.pop())
{
return false;
}
head=head.next;
}

return true;

}

//解法4.(全部反转链表法)判断一个链表是否为回文链表
public static boolean isHuiWenList4(Node head){

if(head==null||head.next==null)
{
return true;
}
Node t=Reverselist(head);
while(head!=null&&t!=null)
{
if(head.value!=t.value)
{
return false;

}
head=head.next;
t=t.next;
}
return true;

}
//(循环解法)翻转链表
public static Node Reverselist(Node head)
{
if(head==null)
{
return null;
}

Node p=head; //记录头节点
if(p.next!=null) //移动第二个节点
{
p=p.next;
}else
{
return head;
}
head.next=null; //头节点的下一个节点指向空

while(p!=null)
{ Node t=p.next;
p.next=head;
head=p;
p=t;

}

return head;
}
//================================================
//解法5.(部分反转)判断一个链表是否为回文链表
public static boolean isHuiWenList5(Node head){

if(head==null||head.next==null)
{
return true;
}

Node n1=head;
Node n2=head;
//查找中间节点
while(n2.next!=null&&n2.next.next!=null)
{

n1=n1.next;
n2=n2.next.next;
}
n2=n1.next; //n2->右半部分第一个节点
n1.next=null;

Node n3=null;

//右半部分反转
while(n2!=null)
{
n3=n2.next; //n3保存以一个节点
n2.next=n1; //下一个反转节点
n1=n2; //n1移动
n2=n3; //n2移动

}
n3=n1; //保存最后一个节点
n2=head; //指向第一个节点
boolean res=true;

//判断回文
while(n1!=null&&n2!=null)
{
if(n1.value!=n2.value)
{
res=false;
break;
}
n1=n1.next; //从左到中部
n2=n2.next; //从右到中部

}
//恢复链表
n1=n3.next;
n3.next=null;
while(n1!=null)
{
n2=n1.next;
n1.next=n3;
n3=n1;
n1=n2;

}
return res;

}

public static void main(String[]args)
{

//System.out.println("Hello");
Node node =new Node(1);
node.next=new Node(2);
node.next.next=new Node(2);
node.next.next.next=new Node(3);

System.out.println(isHuiWenList(node));
System.out.println(isHuiWenList2(node));
System.out.println(isHuiWenList3(node));
//System.out.println(isHuiWenList4(node));
System.out.println(isHuiWenList5(node));
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息