解题思路:
- 该题目最大的好处就在于 - 没有要求空间。那么就happy了,以空间换时间,百试不爽
- 设置一个列表,把链表中所有结点的值保存下来,然后逆切片看等不等
# def __init__(self, x):
# self.val = x
# self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param head ListNode类 the head
# @return bool布尔型
#
class Solution:
def isPail(self , head: ListNode) -> bool:
# write code here
res = []
q = head
while q:
res.append(q.val)
q = q.next
print(res)
if res == res[::-1]:
return True
else:
return False