-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.AddTwoNumbers.py
55 lines (40 loc) · 1.27 KB
/
2.AddTwoNumbers.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
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# # Solution 1
class Solution:
def addTwoNumbers(self, l1, l2):
root = ListNode(0)
result = root
carry = 0
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry += l2.val
l2 = l2.next
result.next = ListNode(carry%10)
result = result.next
carry = carry // 10
return root.next
# # Solution 2
# class Solution:
# def addTwoNumbers(self, l1, l2):
# first_num, second_num = "", ""
# while l1 != None:
# first_num += str(l1.val)
# l1 = l1.next
# while l2 != None:
# second_num += str(l2.val)
# l2 = l2.next
# summation = list(str(int(first_num) + int(second_num)))
# digit_node = None
# for index, value in enumerate(summation):
# if index == 0:
# digit_node = ListNode(val=value)
# continue
# digit_node = ListNode(val=value, next=digit_node)
# return digit_node