-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. Add Two Numbers
More file actions
53 lines (41 loc) · 1.15 KB
/
2. Add Two Numbers
File metadata and controls
53 lines (41 loc) · 1.15 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
42
43
44
45
46
47
48
49
50
51
52
53
# time complexity is o(n) and sc complexity is o(1)
# 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]:
l11 =l1
l22=l2
tail=l1
c=0
while(l1 and l2):
s=l1.val+l2.val+c
c=s//10
s=s%10
print(s)
l1.val=s
if(l1.next != None):
l11=l11.next
l1=l1.next
l2=l2.next
while(l1):
s=c+l1.val
c=s//10
s=s%10
l1.val=s
if(l1.next != None):
l11=l11.next
l1=l1.next
while(l2):
s=c+l2.val
c=s//10
s=s%10
l11.next=ListNode(s)
l11=l11.next
l2=l2.next
if(c>0):
new=ListNode(c)
l11.next=new
return tail