zoukankan      html  css  js  c++  java
  • LeetCode回文链表Swift

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

    示例 1:

    输入: 1->2
    输出: false


    示例 2:

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


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

    思路一:用数组,时间复杂度O(n),空间复杂度O(n)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public var val: Int
     *     public var next: ListNode?
     *     public init(_ val: Int) {
     *         self.val = val
     *         self.next = nil
     *     }
     * }
     */
    class Solution {
        func isPalindrome(_ head: ListNode?) -> Bool {
            if head == nil || head?.next == nil {
                return true
            }
            var head = head
            var stack:[Int] = [(head?.val)!]
            while head?.next != nil {
                stack.append((head?.next!.val)!)
                head = head?.next
            }
            return stack == stack.reversed()
        }
    }

    思路二:快慢指针找中间结点,同时将前半部分反转,然后与剩下的后半部分做比较,时间复杂度O(n),空间复杂度O(1)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public var val: Int
     *     public var next: ListNode?
     *     public init(_ val: Int) {
     *         self.val = val
     *         self.next = nil
     *     }
     * }
     */
    class Solution {
        func isPalindrome(_ head: ListNode?) -> Bool {
            if head == nil || head?.next == nil {
                return true
            }
            //快慢指针寻找中间节点,同时把前半部分反转
            var slow = head, fast = head
            var pre: ListNode? = nil
            
            //若12321,运行完 slow = 321,fast = 1,中间结点为3,pre = 21
            //若1221,运行完 slow = 21,fast = nil,中间结点为第2个2,pre = 21
            while fast != nil && fast?.next != nil {
                let cur = slow
                slow = slow?.next
                fast = fast?.next?.next
                //下面是同时反转前半部分
                cur?.next = pre
                pre = cur
            }
            //链表奇数个时,后半部分略过中间结点
            if fast != nil {
                slow = slow?.next
            }
            //slow后半部分的值和pre反转后的前半部分的值做比较
            while slow != nil && pre != nil {
                if slow?.val != pre?.val {
                    return false
                }
                slow = slow?.next
                pre = pre?.next
            }
            return true
        }
    }
  • 相关阅读:
    创新县(市、区)的主要条件
    C# imgage图片转base64字符/base64字符串转图片另存成
    base64编码的 文件 图片
    CSS
    Exception has been thrown by the target of an invocation 网站报错
    高效通用分页存储过程 多表查询
    JavaScript 知识记录
    JQuery 全选 取消
    ELK(elasticsearch+logstash+kibana)入门到熟练-从0开始搭建日志分析系统教程
    ELK- elasticsearch 讲解,安装,插件head,bigdesk ,kopf,cerebro(kopf升级版)安装
  • 原文地址:https://www.cnblogs.com/huangzs/p/14251667.html
Copyright © 2011-2022 走看看