-
Notifications
You must be signed in to change notification settings - Fork 0
/
55_Add_Two_Numbers_II
54 lines (54 loc) · 1.43 KB
/
55_Add_Two_Numbers_II
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
44
45
46
47
48
49
50
51
52
53
54
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
def reverse(head):
curr=head
prev=None
nex=None
while curr!=None:
nex=curr.next
curr.next=prev
prev=curr
curr=nex
return prev
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
l1=reverse(l1)
l2=reverse(l2)
carry=0
ans=ListNode()
ptr3=ans
ptr1=l1
ptr2=l2
while ptr1 or ptr2:
if ptr1 and ptr2:
s=ptr1.val+ptr2.val+carry
carry=s//10
s=s%10
temp=ListNode(s)
ptr3.next=temp
ptr3=ptr3.next
ptr1=ptr1.next
ptr2=ptr2.next
elif ptr1:
s=ptr1.val+carry
carry=s//10
s=s%10
temp=ListNode(s)
ptr3.next=temp
ptr3=ptr3.next
ptr1=ptr1.next
elif ptr2:
s=ptr2.val+carry
carry=s//10
s=s%10
temp=ListNode(s)
ptr3.next=temp
ptr3=ptr3.next
ptr2=ptr2.next
if carry:
temp=ListNode(carry)
ptr3.next=temp
return reverse(ans.next)