【leetcode 简单】 第六十七题 回文链表

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

示例 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 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
上一篇:js倒计时,秒倒计时,天倒计时


下一篇:Oracle PL/SQL 编程基础 实例