其他分享
首页 > 其他分享> > 234.回文链表。双百0ms

234.回文链表。双百0ms

作者:互联网

先找到中点,然后对后半部分反转,从头和中点进行比较是否相同

class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode slow = head, fast = head, prev = null;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        while(slow != null){
            ListNode temp = slow.next;
            slow.next = prev;
            prev = slow;
            slow = temp;
        }
        while(head != null && prev != null){
            if(head.val != prev.val) return false;
            head = head.next;
            prev = prev.next;
        }
        return true;
    }
}

标签:head,slow,0ms,null,fast,next,链表,234,prev
来源: https://blog.csdn.net/Split_token/article/details/122523427