-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_2.py
More file actions
53 lines (43 loc) · 1.19 KB
/
question_2.py
File metadata and controls
53 lines (43 loc) · 1.19 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/10 11:40
# @Author : cancan
# @File : question_2.py
# @Function : 两数相加
"""
Question:
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。
将两数相加返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。
Example:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
"""
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
def f(node):
num = 0
d = 0
t = node
while t:
num += t.val * 10 ** d
d += 1
t = t.next
return num
num = str(f(l1) + f(l2))
r = ListNode(int(num[-1]))
t = r
for i in num[:-1][::-1]:
t.next = ListNode(int(i))
t = t.next
return r