-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Add-Two-Numbers.py
More file actions
37 lines (34 loc) · 1.46 KB
/
Copy path2-Add-Two-Numbers.py
File metadata and controls
37 lines (34 loc) · 1.46 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
# 2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/
# level: medium
# complexity: O(n) time, O(1) space
# 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
"""
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1: # to make sure that the node isn't None
carry += l1.val
l1 = l1.next
if l2: # to make sure that the node isn't None
carry += l2.val
l2 = l2.next
carry, val = divmod(carry, 10) # returns a pair of numbers (a tuple) consisting of their quotient and remainder.
# example: divmod(8, 10) = (0, 8)
# At the start, both root and n are assigned to point towards the same ListNode.
# This same ListNode is updated in the while loop via n,
# and in the end we refer to the start of the ListNode (but skipping the head)
# using root.next.
# We use root.next instead of root to skip the dummy head of value 0.
n.next = n = ListNode(val) # first n.next = ListNode(val) then n point to the same address
return root.next