leetcode 234 回文链表 ---

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

示例 1:

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

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

# 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
        """
        if not head:
            return True
        res = []
        if head == None:
            return None
        while head:
            res.append(head.val)
            head = head.next
        return res == res[::-1]

提交结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40924580/article/details/84206432