-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.nim
56 lines (43 loc) · 1.28 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import std/[strutils, algorithm]
type
Node[int] = ref object
value: int
next: Node[int]
SinglyLinkedList[T] = object
head, tail: Node[T]
proc append[T](list: var SinglyLinkedList[T], data: T = nil): void =
var node = Node[T](value: data)
if list.head.isNil:
list.head = node
list.tail = node
else:
list.tail.next = node
list.tail = node
proc preview[T](list: SinglyLinkedList[T]): string =
var s: seq[T]
var n = list.head
while not n.isNil:
s.add n.value
n = n.next
result = s.join(" -> ")
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
var list1: SinglyLinkedList[int]
var list2: SinglyLinkedList[int]
for i in @[2, 4, 3]: list1.append(i)
for i in @[5, 6, 4]: list2.append(i)
echo(preview(list1))
echo(preview(list2))
echo(preview(addTwoNumbers(list1, list2)))