随笔- 177  文章- 0  评论- 0  阅读- 1273 

                                                                                              

                                   📑打牌 : 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   dapaige  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示