LeetCode234 回文链表

题目

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

示例 1:

输入: 1->2
输出: false
示例 2:

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

代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if head is None:
            return True
        cur = head
        array = []
        while cur:
            array.append(cur.val)
            cur = cur.next
        return array== array[::-1]

猜你喜欢

转载自blog.csdn.net/lgy54321/article/details/85056017
今日推荐