-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwoNumbers.py
More file actions
34 lines (29 loc) · 1014 Bytes
/
addTwoNumbers.py
File metadata and controls
34 lines (29 loc) · 1014 Bytes
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
# problem: https://leetcode.com/problems/add-two-numbers-ii/submissions/
# Runtime: 68 ms, faster than 88.41% of Python3 online submissions for Add Two Numbers II.
# Memory Usage: 14.2 MB, less than 100.00% of Python3 online submissions for Add Two Numbers II.
# 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: ListNode, l2: ListNode) -> ListNode:
sumOne = 0
sumTwo = 0
while (l1 or l2):
if l1:
sumOne = (sumOne * 10) + l1.val
l1 = l1.next
if l2:
sumTwo = (sumTwo * 10) + l2.val
l2 = l2.next
total = sumOne + sumTwo
cur = None
if total == 0:
head = ListNode()
while total:
digit = total % 10
total //= 10
head = ListNode(digit, cur)
cur = head
return head