diff --git a/solution/0002.Add Two Numbers/Solution2.py b/solution/0002.Add Two Numbers/Solution2.py new file mode 100644 index 0000000000000..2f6fecbde3884 --- /dev/null +++ b/solution/0002.Add Two Numbers/Solution2.py @@ -0,0 +1,16 @@ +class Solution: + """ + This solution is from: https://leetcode.com/problems/add-two-numbers/discuss/1102/Python-for-the-win + A very Pythonic solution, coooooool! + Runtime: 76 ms, faster than 84.66% of Python3 online submissions for Add Two Numbers. + Memory Usage: 13.3 MB, less than 38.91% of Python3 online submissions for Add Two Numbers. + """ + def addTwoNumbers(self, l1, l2): + def toint(node): + return node.val + 10 * toint(node.next) if node else 0 + def tolist(n): + node = ListNode(n % 10) + if n > 9: + node.next = tolist(n // 10) + return node + return tolist(toint(l1) + toint(l2)) diff --git a/solution/README.md b/solution/README.md index 7430ec11368b4..9fa7f01548b22 100644 --- a/solution/README.md +++ b/solution/README.md @@ -19,6 +19,7 @@ │   ├── Solution.js │   ├── Solution.py │   ├── Solution.rb +│   ├── Solution2.py │   └── Solution2.js ├── 0003.Longest Substring Without Repeating Characters │   ├── README.md @@ -1274,4 +1275,4 @@ ├── 5087.Letter Tile Possibilities │   └── Solution.py └── README.md -``` \ No newline at end of file +```