Use post-order traversal to check the input string recursively.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
left = 0
def postorder(node, right):
nonlocal head
nonlocal left
if node:
tmp = postorder(node.next, right + 1)
if not tmp:
return tmp
if left < right:
if node.val != head.val:
return False
head = head.next
left += 1
return True
return True
return postorder(head, 0)
No comments:
Post a Comment