Python 10 line O(1) space and O(n) time solution


  • 0
    F
    class Solution(object):
        def isPalindrome(self, head):
            node, result = isPalindromHelper(head, head)
            return result 
        
    def isPalindromHelper(forward, backward):
        if not forward or not backward:
            return (None,True)
        if backward and not backward.next:
            return (forward.next, forward.val == backward.val)
        node, bool_val = isPalindromHelper(forward, backward.next)
        return (node.next, node.val == backward.val and bool_val)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.