Skip to content

Commit f67fc91

Browse files
authored
Merge pull request doocs#201 from FatEagle/master
A pythonic solution for 0002
2 parents d1a9328 + 3834ae5 commit f67fc91

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
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))

solution/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
│   ├── Solution.js
2020
│   ├── Solution.py
2121
│   ├── Solution.rb
22+
│   ├── Solution2.py
2223
│   └── Solution2.js
2324
├── 0003.Longest Substring Without Repeating Characters
2425
│   ├── README.md
@@ -1274,4 +1275,4 @@
12741275
├── 5087.Letter Tile Possibilities
12751276
│   └── Solution.py
12761277
└── README.md
1277-
```
1278+
```

0 commit comments

Comments
 (0)