• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode234.回文链表|PalindromeLinkedList

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9748159.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a singly linked list, determine if it is a palindrome.

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Follow up:
Could you do it in O(n) time and O(1) space?


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

示例 1:

输入: 1->2
输出: false

示例 2:

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

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


 72ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func isPalindrome(_ head: ListNode?) -> Bool {
14         var root = head
15         var stack = [ListNode]()
16         while root != nil {
17             stack.append(root!)
18             root = root?.next
19         }
20         
21         var low = 0
22         var high = stack.count - 1
23         
24         while low < high {
25             let lownode = stack[low]
26             let highnode = stack[high]
27             
28             if lownode.val == highnode.val {
29                 low += 1
30                 high -= 1
31             } else {
32                 return false
33             }
34         }
35         return true
36     }
37 }

56ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func isPalindrome(_ head: ListNode?) -> Bool {
14         var slow = head
15         var fast = head
16         var reverse: ListNode? = nil
17         
18         while fast?.next != nil {
19             fast = fast!.next!.next
20             let next = slow!.next
21             slow!.next = reverse
22             reverse = slow
23             slow = next
24         }
25         if fast != nil {
26             slow = slow!.next
27         }
28         
29         while slow != nil {
30             if slow!.val != reverse!.val {
31                 return false
32             }
33             slow = slow!.next
34             reverse = reverse!.next
35         }
36         
37         return true
38     }
39 }

60ms

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     public var val: Int
 5  *     public var next: ListNode?
 6  *     public init(_ val: Int) {
 7  *         self.val = val
 8  *         self.next = nil
 9  *     }
10  * }
11  */
12 class Solution {
13     func isPalindrome(_ head: ListNode?) -> Bool {
14         var slow = head
15         var fast = head
16         var reverse: [Int] = [Int]()
17         reverse.reserveCapacity(100)
18         
19         while fast?.next != nil {
20             fast = fast!.next!.next
21             reverse.append((slow?.val)!)
22             slow = slow?.next
23         }
24         if fast != nil {
25             slow = slow!.next
26         }
27         
28         while slow != nil {
29             if slow!.val != reverse.popLast() {
30                 return false
31             }
32             slow = slow!.next
33         }
34         
35         return true
36     }
37 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift2.1keyword @noescape介绍发布时间:2022-07-13
下一篇:
iOS/swift单选框和复选框发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap