-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2AddTwoNumbers.py
32 lines (29 loc) · 1.02 KB
/
Q2AddTwoNumbers.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
# @b-knd (jingru) on 07 August 2022 15:14:00
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
dummyHead = ListNode(0)
curr = dummyHead
carry = 0
while l1 != None or l2 != None or carry > 0:
x = l1.val if l1 else 0
y = l2.val if l2 else 0
currSum = x + y + carry
newNode = ListNode(currSum % 10)
carry = currSum // 10
curr.next = newNode
curr = curr.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummyHead.next
#Runtime: 84 ms, faster than 54.07% of Python online submissions for Add Two Numbers.
#Memory Usage: 13.6 MB, less than 17.39% of Python online submissions for Add Two Numbers.