链表反转,末尾加 1

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @return ListNode类
#
class Solution:
    def reverse(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head
        t = None
        while head:
            n = head.next
            head.next = t
            t = head
            head = n
        return t
             
    def plusOne(self, head: ListNode) -> ListNode:
        # write code here
        first, flag = True, 0
        new_head = self.reverse(head)
        pre = ListNode(-1)
        pre2 = pre
        while new_head:
            if first:
                val = new_head.val + 1
                first = False
            else:
                val = new_head.val
            if flag:
                val += 1
                flag = 0
            if val > 9:
                val -= 10
                flag = 1
            node = ListNode(val)
            pre.next = node
            pre = pre.next
            new_head = new_head.next
        if flag:
            pre.next = ListNode(1)
            flag = 0
        return self.reverse(pre2.next)