Simple Python solution


  • 0
    W
    class Solution(object):
        def plusOne(self, head):
            carry = self._plusOne(head)
            if carry > 0:
                new_head = ListNode(carry)
                new_head.next = head
                return new_head
            return head
            
        def _plusOne(self, head):
            if head is None:
                return 1
            carry = self._plusOne(head.next)
            carry, reminder = divmod(head.val + carry, 10)
            head.val = reminder
            return carry
    

Log in to reply
 

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