forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.nim
31 lines (25 loc) · 794 Bytes
/
Solution.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#[
# Driver code in the solution file
# Definition for singly-linked list.
type
Node[int] = ref object
value: int
next: Node[int]
SinglyLinkedList[T] = object
head, tail: Node[T]
]#
# More efficient code churning ...
proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] =
var
aggregate: SinglyLinkedList
psum: seq[char]
temp_la, temp_lb: seq[int]
while not l1.head.isNil:
temp_la.add(l1.head.value)
l1.head = l1.head.next
while not l2.head.isNil:
temp_lb.add(l2.head.value)
l2.head = l2.head.next
psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt()))
for i in psum: aggregate.append(($i).parseInt())
result = aggregate