题目描述
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
题目理解
将链表的结点的值放入到列表中
翻转列表
判断翻转列表后是否和之前未反转一样
实现代码
class Solution:
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is None or head.next is None:
return True
l = []
p = head
while p.next:
l.append(p.val)
p = p.next
l.append(p.val)
return l == l[::-1]