Created
December 17, 2018 14:01
-
-
Save mylons/1bc8df455004130d56e2776b8c6abbe6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Definition for singly-linked list. | |
| class ListNode: | |
| def __init__(self, x): | |
| self.val = x | |
| self.next = None | |
| def list_to_stack(l): | |
| s = [] | |
| node = l | |
| while node: | |
| s.append(node.val) | |
| node = node.next | |
| return s | |
| def stack_to_num(s): | |
| num = 0 | |
| while len(s) > 0: | |
| num = (num * 10) + s.pop() | |
| return num | |
| class Solution: | |
| def addTwoNumbers(self, l1, l2): | |
| """ | |
| :type l1: ListNode | |
| :type l2: ListNode | |
| :rtype: ListNode | |
| """ | |
| return stack_to_num(list_to_stack(l1)) + stack_to_num(list_to_stack(l2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment