-
Notifications
You must be signed in to change notification settings - Fork 17
/
plus-one-linked-list.py
43 lines (31 loc) · 1.01 KB
/
plus-one-linked-list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def plusOne(self, head: ListNode) -> ListNode:
if not head:
return ListNode(1)
def reverse_list(head: ListNode) -> ListNode:
prev, cur = head, head.next
prev.next = None
new_head = prev
while cur:
new_head = cur
cur_next = cur.next
cur.next = prev
prev, cur = cur, cur_next
return new_head
new_head = reverse_list(head)
node = new_head
carry = 1
while node and carry > 0:
node.val, carry = (node.val + carry) % 10, (node.val + carry) // 10
node = node.next
new_head = reverse_list(new_head)
if carry > 0:
carry_head = ListNode(1)
carry_head.next = new_head
new_head = carry_head
return new_head