Leetcode 234：回文链接列表 [英] Leetcode 234: Palindrome LinkedList

问题描述

``````class Solution:
def isPalindrome(self, head: ListNode) -> bool:
#Null condition
return True

#move fast and slow pointers
while(f_p.next != None and f_p.next.next != None):
s_p = s_p.next
f_p = f_p.next.next

#reverse slow pointer
first_half = self.reverse(s_p)

#compare
while(first_half != None and head != None):
print(first_half.val)
return False
first_half = first_half.next

return True

def reverse(self,c_p):

while(c_p != None):
temp = c_p.next
c_p = temp

``````

``````class Solution:
def isPalindrome(self, head: ListNode) -> bool:
#Null condition
return True

#move fast and slow pointers
while(f_p.next != None and f_p.next.next != None):
s_p = s_p.next
f_p = f_p.next.next

#set s_p.next to Null
s_p.next = None

#reverse fast pointer
second_half = self.reverse(f_p)

#compare
while(second_half != None and head != None):
print(second_half.val)
return False
second_half = second_half.next

return True

def reverse(self,c_p):

while(c_p != None):
temp = c_p.next
c_p = temp

``````

推荐答案

Leetcode版本解决方案将起始值与反转后半部分的值进行比较。