## Description

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Example:

Input: (7 -> 1 -> 6) + (5 -> 9 -> 2). That is, 617 + 295.
Output: 2 -> 1 -> 9. That is, 912.


Follow Up: Suppose the digits are stored in forward order. Repeat the above problem.

Example:

Input: (6 -> 1 -> 7) + (2 -> 9 -> 5). That is, 617 + 295.
Output: 9 -> 1 -> 2. That is, 912.


## Solutions

### 1. Stack + direct

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
value = self.ll_to_value(l1) + self.ll_to_value(l2)
dummy = ListNode(0)
node = dummy
while value !=0:
value, mod = divmod(value, 10)
node.next = ListNode(mod)
node = node.next
return dummy.next if node != dummy else dummy

def ll_to_value(self, ll):
if not ll:
return 0
node = ll
res = 0
stack = []
while node:
stack.append(node.val)
node = node.next
while stack:
res = res * 10 + stack.pop()
return res