zoukankan      html  css  js  c++  java
  • Medium | LeetCode 234. 回文链表

    234. 回文链表

    请判断一个链表是否为回文链表。

    示例 1:

    输入: 1->2
    输出: false
    

    示例 2:

    输入: 1->2->2->1
    输出: true
    

    进阶:
    你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

    解题思路

    将后半部分链表反转, 然后设置双指针, 同时遍历。遍历完成后再将链表还原。

    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        ListNode mid = getMid(head);
        ListNode reverseHalf = reverseList(mid);
        ListNode preHalf = head;
        ListNode postHalf = reverseHalf;
        boolean flag = true;
        while(preHalf != null && postHalf != null) {
            if (preHalf.val != postHalf.val) {
                return false;
            }
            preHalf = preHalf.next;
            postHalf = postHalf.next;
        }
        reverseList(reverseHalf);
        return flag;
    }
    
    // 当总个数为偶数时, 返回后面一半的第一个元素
    // 当总个数为奇数时, 返回中间元素
    public ListNode getMid(ListNode head) {
        ListNode slow, fast;
        slow = fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
    
        ListNode preNode = null;
        ListNode currentNode = head;
        ListNode nextNode = currentNode.next;
        while (nextNode != null) {
            currentNode.next = preNode;
            preNode = currentNode;
            currentNode = nextNode;
            nextNode = nextNode.next;
        }
        currentNode.next = preNode;
        return currentNode;
    }
    
  • 相关阅读:
    Spring Mvc和Mybatis的多数据库访问配置过程
    Git下解决冲突
    安装Git
    数据库优化
    Ubuntu版 微信
    ssh框架简介
    写代码的习惯
    CentOS 7 安装 docker 并搭建私有仓库
    IPv4地址分类及特征
    Xcode 7.0 Could not find developer disk image
  • 原文地址:https://www.cnblogs.com/chenrj97/p/14295434.html
Copyright © 2011-2022 走看看