Python easy understand solution


  • 0
    L
    class Solution(object):
    
        def plusOne(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:return head
            carry = 0
            cur = self.reverse(head)
            origin = cur
            cur.val +=1
            while cur.val >=10 and cur.next:
                carry = cur.val /10
                cur.val = cur.val %10
                cur= cur.next
                cur.val += carry
                
            if cur.val >=10:
                cur.next=ListNode(1)
                cur.val = int(cur.val %10)
                cur=cur.next
            head = self.reverse(origin)
            return head
            
        def reverse(self,head):
            tail=None
            while head:
                head.next,head,tail=tail,head.next,head
            return tail
            ```

Log in to reply
 

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