-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Add-Two-Numbers.py
More file actions
41 lines (39 loc) · 1.24 KB
/
2-Add-Two-Numbers.py
File metadata and controls
41 lines (39 loc) · 1.24 KB
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
ptr1, ptr2 = l1, l2
li = ListNode()
ptr = li
remainder = 0
while ptr1 or ptr2:
if ptr1 == None:
num1 = 0
num2 = ptr2.val
elif ptr2 == None:
num1 = ptr1.val
num2 = 0
else:
num1 = ptr1.val
num2 = ptr2.val
if num1 + num2 + remainder > 9:
sum = (num1 + num2 + remainder) % 10
ptr.next = ListNode(sum)
ptr = ptr.next
remainder = ((num1 + num2 + remainder) - sum) // 10
else:
sum = num1 + num2 + remainder
ptr.next = ListNode(sum)
remainder = 0
ptr = ptr.next
if ptr1:
ptr1 = ptr1.next
if ptr2:
ptr2 = ptr2.next
if remainder:
ptr.next = ListNode(remainder)
li = li.next
return li