//链表是否是回文 找到链表的中间节点 反转单链表 head从后往后走 slow从后往前走
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(slow!=this.head){
if(slow.data!=head.data){
return false;
}
//节点为偶数
if(this.head.next==slow){
return true;
}
head=head.next;
slow=slow.next;
}
return true;
}
链表是否是回文
最新推荐文章于 2022-11-02 21:27:39 发布