-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path445_add2numbers.py
40 lines (35 loc) · 1012 Bytes
/
445_add2numbers.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def insert2Stack(self, list):
stack = []
while list:
stack.append(list.val)
list = list.next
return stack
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
stack1 = self.insert2Stack(l1)
stack2 = self.insert2Stack(l2)
sum = 0
head = ListNode(0)
while stack1 or stack2:
sum += stack1.pop() if stack1 else 0
sum += stack2.pop() if stack2 else 0
head.val = sum % 10
newNode = ListNode(sum/10)
newNode.next = head
head = newNode
sum = sum/10
if head.val == 0:
head = head.next
return head