-
Notifications
You must be signed in to change notification settings - Fork 0
/
002-Add-Two-Numbers.py
38 lines (30 loc) · 1.08 KB
/
002-Add-Two-Numbers.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1, l2):
# create a Answer node and mark it's start node
StartAns = Answer = ListNode(0)
carry = 0
# keep counting until one of nodes is end
while l1 and l2:
carry, sumval = divmod( l1.val+l2.val+carry, 10 )
Answer.next = ListNode( sumval )
# to next node
l1 = l1.next
l2 = l2.next
Answer = Answer.next
# if l1 or l2 still have next node
tmplist = l1 or l2
while tmplist:
carry, sumval = divmod( tmplist.val+carry, 10 )
Answer.next = ListNode( sumval )
# to next node
tmplist = tmplist.next
Answer = Answer.next
if carry != 0:
Answer.next = ListNode(carry)
#return without the first one node
return StartAns.next