📑打牌 : da pai ge的个人主页
                                   🌤️个人专栏 : da pai ge的博客专栏
                                   ☁️宝剑锋从磨砺出,梅花香自苦寒来

 🌤️题目结构

给定一个链表,请判断该链表是否为回文结构。

回文是指该字符串正序逆序完全一致。

☁️题目示例

示例1

输入:

{1}

复制返回值:

true

复制

示例2

输入:

{2,1}

复制返回值:

false

复制说明:

2->1     

 📑相关代码和图解

 @param head ListNode类 the head

     * @return bool布尔型

     */

    public boolean isPail (ListNode head) {

        if (head == null) {

            return false;

        }

        if (head.next == null) {

            return true;

        }

        // write code here

        ListNode fast = head;

        ListNode slow = head;

        while (fast != null && fast.next != null) {



            fast = fast.next.next;

            slow = slow.next;

        }



        ListNode cur = slow.next;

        while (cur != null) {

            ListNode curNext = cur.next;

            cur.next = slow;

            slow = cur;

            cur = curNext;

        }



        while (slow != head) {

            if (slow.val != head.val) {

                return false;

            }

            if (head.next == slow) {

                return true;

            }

            slow = slow.next;

            head = head.next;



        }




        return true;

 posted on 2023-12-05 11:35  dapaige  阅读(14)  评论(0)    收藏  举报  来源