leetcode之Palindrome Linked List(234)

题目:

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

示例 1:

输入: 1->2
输出: false

示例 2:

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

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

python代码:

class Solution(object):
    def isPalindrome(self, head):
        if head == None or head.next == None:
            return True
        l = []
        while head:
            l.append(head.val)
            head = head.next
        return l == l[::-1]

心得:在大多数回文问题上,如果用python解题,可以往列表方向考虑,然后return list == list[::-1]即可.

猜你喜欢

转载自blog.csdn.net/cuicheng01/article/details/81390293
今日推荐