Skip to content

Commit 350de16

Browse files
committed
a very pythonic solution
1 parent d1a9328 commit 350de16

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
"""
3+
This solution is from: https://leetcode.com/problems/add-two-numbers/discuss/1102/Python-for-the-win
4+
A very Pythonic solution, coooooool!
5+
Runtime: 76 ms, faster than 84.66% of Python3 online submissions for Add Two Numbers.
6+
Memory Usage: 13.3 MB, less than 38.91% of Python3 online submissions for Add Two Numbers.
7+
"""
8+
def addTwoNumbers(self, l1, l2):
9+
def toint(node):
10+
return node.val + 10 * toint(node.next) if node else 0
11+
def tolist(n):
12+
node = ListNode(n % 10)
13+
if n > 9:
14+
node.next = tolist(n // 10)
15+
return node
16+
return tolist(toint(l1) + toint(l2))

0 commit comments

Comments
 (0)