-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (26 loc) · 795 Bytes
/
main.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverse(self, head):
a, b = None, head
while b is not None:
c = b.next
b.next = a
a, b = b, c
return a
def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(next=self.reverse(head))
carry = 0
p = dummy
while p.next is not None:
p.next.val = p.next.val * 2 + carry
carry = p.next.val // 10
p.next.val %= 10
p = p.next
if carry > 0:
p.next = ListNode(carry)
p = p.next
return self.reverse(dummy.next)