请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
利用数组判断
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
nums = []
while head:
stack.append(head.val)
head = head.next
return nums == nums[::-1]
快慢指针
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
prev = None
fast = slow = head
# 翻转链表的前n/2个结点
while fast and fast.next:
fast = fast.next.next
prev, prev.next, slow = slow, prev, slow.next
# 结点个数为奇数时,跳过最中间的结点
if fast:
slow = slow.next
# 前n/2个结点翻转后,与剩下的结点进行对比
# 移动指针,
while slow and slow.val == prev.val:
# 两个节点同时后移
prev, slow = prev.next, slow.next
return not prev