请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
# Definition for singly- ed list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def reverst(self,head): a=head b=head.next head.next = None; while b: c = b.next b.next = a a = b b=c head = a return head def isPalindrome(self, head): """ :type head: ListNode :rtype: bool """ if not head or not head.next: return True fast = slow = head while fast.next and fast.next.next: slow = slow.next fast = fast.next.next slow = slow.next slow = self.reverst(slow) while slow: if head.val != slow.val: return False slow = slow.next head = head.next return True
继续阅读与本文标签相同的文章
-
阿里云 MVP 全球闭门会 2019 荣聚云栖小镇,从心出发!
2026-05-18栏目: 教程
-
Spring AOP应用场景你还不知道?这篇一定要看!
2026-05-18栏目: 教程
-
阿里云云数据库RDS如何监控、备份及克隆实例?
2026-05-18栏目: 教程
-
用CSS新属性实现特殊的图片显示效果
2026-05-18栏目: 教程
-
Java描述设计模式(12):外观模式
2026-05-18栏目: 教程
