编写一个函数,检查输入的链表是否是回文的。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
# 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
"""
res=[]
while head:
res.append(head.val)
head=head.next
return res==res[::-1]
执行结果:
通过
显示详情
执行用时 :64 ms, 在所有 Python 提交中击败了80.89%的用户
内存消耗 :32.1 MB, 在所有 Python 提交中击败了100.00%的用户