Weird corner case

  • 0

    My code is working for all test cases except when the input is just a stream of 9s, in which case the output shows only [1]. I tried printing the value of before returning the node and it seems to be printing the output correctly. Could someone please explain what is going wrong?

    def plusOne(self, head):
        c = self.helper(head)
        if not c:
            return head
            dummy = TreeNode(c)
   = head
            # print dummy.val,
            return dummy
    def helper(self, node):
        if not node:
            return 1
        c = self.helper(
        c, node.val = (node.val+c)//10, (node.val+c)%10
        return c

  • 1

    @kvsidharth Hi, not sure why the compiler doesn't complain, but you have a typo, "TreeNode" should be "ListNode" shouldn't it?

  • 0

    @Ipeq1 I think leetcode has the all those classes predefined for any code and hence didn't complain about its existence. My code gets accepted with me correcting the typo. Thanks a lot for the observation.

Log in to reply

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