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 . I tried printing the value of node.next 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 else: dummy = TreeNode(c) dummy.next = head # print dummy.val, dummy.next.val return dummy def helper(self, node): if not node: return 1 c = self.helper(node.next) c, node.val = (node.val+c)//10, (node.val+c)%10 return c
@kvsidharth Hi, not sure why the compiler doesn't complain, but you have a typo, "TreeNode" should be "ListNode" shouldn't it?
@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.