public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
if(A==null) {
return true;
}
ListNode fast = A;
ListNode slow = fast;
ListNode cur = fast;
while(fast!=null&&fast.next!=null) {
fast = fast.next.next;
slow = slow.next;
}
cur=slow.next;
while(cur!=null){
ListNode curNext = cur.next;
cur.next = slow;
slow=cur;
cur=curNext;
}
while(A!=slow) {
if(A.val!=slow.val){
return false;
}
if(A.next!=slow){
return true;
}
A=A.next;
slow=slow.next;
}
return true;
}
}
判断链表是否回文
最新推荐文章于 2024-08-19 23:56:23 发布