-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproblem_2.py
112 lines (106 loc) · 3.21 KB
/
problem_2.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Problem 2. Add two numbers (Medium): https://leetcode.com/problems/add-two-numbers/
# 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, l2):
num1 = 0
factor = 1
head = ListNode()
while(l1):
num1 = num1 + l1.val * factor
factor *= 10
l1 = l1.next
num2 = 0
factor = 1
while(l2):
num2 = num2 + l2.val * factor
factor *= 10
l2 = l2.next
result = num1 + num2
prev = head
if(result == 0):
return head
while(result != 0):
value = result % 10
result = result // 10
prev.next = ListNode(value)
prev = prev.next
return head.next
# Optimized solution
class Solution2:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
head = None
current = None
carry = 0
while(l1 and l2):
val = l1.val + l2.val + carry
if(head):
current.next = ListNode(val % 10)
current = current.next
else:
head = ListNode(val % 10)
current = head
carry = val//10
l1 = l1.next
l2 = l2.next
if(l1):
while(l1):
val = l1.val + carry
carry = val // 10
current.next = ListNode(val%10)
current = current.next
l1 = l1.next
else:
while(l2):
val = l2.val + carry
carry = val // 10
current.next = ListNode(val%10)
current = current.next
l2 = l2.next
if(carry):
current.next = ListNode(carry)
current = current.next
return head
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution3:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
carry = 0
result = None
tail = None
while(l1 and l2):
s = l1.val + l2.val + carry
carry = s // 10
s = s % 10
if(result is None):
result = ListNode(s)
tail = result
else:
tail.next = ListNode(s)
tail = tail.next
l1 = l1.next
l2 = l2.next
print("one or both ended: carry=", carry)
if(l1 and l2 is None):
while(l1):
s = l1.val + carry
carry = s // 10
tail.next = ListNode(s % 10)
tail = tail.next
l1 = l1.next
if(l2 and l1 is None):
while(l2):
s = l2.val + carry
carry = s // 10
tail.next = ListNode(s % 10)
tail = tail.next
l2 = l2.next
if(carry > 0):
tail.next = ListNode(carry)
return result