Skip to content

Instantly share code, notes, and snippets.

@mylons
Created December 17, 2018 14:01
Show Gist options
  • Select an option

  • Save mylons/1bc8df455004130d56e2776b8c6abbe6 to your computer and use it in GitHub Desktop.

Select an option

Save mylons/1bc8df455004130d56e2776b8c6abbe6 to your computer and use it in GitHub Desktop.
# 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