题目说要时间复杂度O(n),空间复杂度O(1)的解法,我没做出来。。。
我的解法就是简单的把问题转化为回文数列:速度倒是很快,就是空间占用很高使用了一个result保存所有值
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
result = []
while head is not None:
result.append(head.val)
head = head.next
return result == result[::-1]