# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return ListNode类
#
class Solution:
    def plusOne(self , head: ListNode) -> ListNode:
        # write code here 用数组也可以做,更简单,但空间复杂度
        pre = None
        while head:#链表反转便于加1
            tmp = head.next
            head.next, pre, head = pre, head, tmp
        dec, cur = 1, pre
        while cur:#反转后的链表积极性加1操作
            dec, val = divmod(dec+cur.val,10)#进位,当前节点值
            cur.val = val
            if dec==0:
                break
            cur = cur.next
        while pre:#链表再次进行反转
            tmp = pre.next
            pre.next, head, pre = head, pre, tmp
        if dec:#若进位符不为0,则在链表头部加上进位节点
            cur = head
            head = ListNode(dec)
            head.next = cur
        return head#返回加1后的链表