这道题一开始想到的比较简单。但是follow up的比较难,暂时没有想出来如何出现O(1)的space。代码如下:
# 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
"""
if head == None:
return True
b = []
while head.next != None:
b.append(head.val)
head = head.next
b.append(head.val)
if b == b[::-1]:
return True
else:
return False
本文探讨了如何通过巧妙的方法,在不使用额外空间的情况下,解决链表问题,具体针对一个判断链表是否为回文的问题进行深入分析,并提供了优化后的代码实现。
108

被折叠的 条评论
为什么被折叠?



