Skip to content

Commit 5b9a614

Browse files
authored
feat: Nim solution for add two numbers (#502)
1 parent a99fde7 commit 5b9a614

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

solution/0000-0099/0002.Add Two Numbers/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,43 @@ class Solution {
304304
}
305305
```
306306

307+
### **Nim**
308+
309+
```nim
310+
#[
311+
# Driver code in the solution file
312+
# Definition for singly-linked list.
313+
type
314+
Node[int] = ref object
315+
value: int
316+
next: Node[int]
317+
318+
SinglyLinkedList[T] = object
319+
head, tail: Node[T]
320+
]#
321+
322+
# More efficient code churning ...
323+
proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] =
324+
var
325+
aggregate: SinglyLinkedList
326+
psum: seq[char]
327+
temp_la, temp_lb: seq[int]
328+
329+
while not l1.head.isNil:
330+
temp_la.add(l1.head.value)
331+
l1.head = l1.head.next
332+
333+
while not l2.head.isNil:
334+
temp_lb.add(l2.head.value)
335+
l2.head = l2.head.next
336+
337+
psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt()))
338+
for i in psum: aggregate.append(($i).parseInt())
339+
340+
result = aggregate
341+
```
342+
343+
307344
### **...**
308345

309346
```

solution/0000-0099/0002.Add Two Numbers/README_EN.md

+36
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,42 @@ class Solution {
292292
}
293293
```
294294

295+
### **Nim**
296+
297+
```nim
298+
#[
299+
# Driver code in the solution file
300+
# Definition for singly-linked list.
301+
type
302+
Node[int] = ref object
303+
value: int
304+
next: Node[int]
305+
306+
SinglyLinkedList[T] = object
307+
head, tail: Node[T]
308+
]#
309+
310+
# More efficient code churning ...
311+
proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] =
312+
var
313+
aggregate: SinglyLinkedList
314+
psum: seq[char]
315+
temp_la, temp_lb: seq[int]
316+
317+
while not l1.head.isNil:
318+
temp_la.add(l1.head.value)
319+
l1.head = l1.head.next
320+
321+
while not l2.head.isNil:
322+
temp_lb.add(l2.head.value)
323+
l2.head = l2.head.next
324+
325+
psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt()))
326+
for i in psum: aggregate.append(($i).parseInt())
327+
328+
result = aggregate
329+
```
330+
295331
### **...**
296332

297333
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import std/[strutils, algorithm]
2+
3+
type
4+
Node[int] = ref object
5+
value: int
6+
next: Node[int]
7+
8+
SinglyLinkedList[T] = object
9+
head, tail: Node[T]
10+
11+
proc append[T](list: var SinglyLinkedList[T], data: T = nil): void =
12+
var node = Node[T](value: data)
13+
if list.head.isNil:
14+
list.head = node
15+
list.tail = node
16+
else:
17+
list.tail.next = node
18+
list.tail = node
19+
20+
proc preview[T](list: SinglyLinkedList[T]): string =
21+
var s: seq[T]
22+
var n = list.head
23+
while not n.isNil:
24+
s.add n.value
25+
n = n.next
26+
result = s.join(" -> ")
27+
28+
proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] =
29+
var
30+
aggregate: SinglyLinkedList
31+
psum: seq[char]
32+
temp_la, temp_lb: seq[int]
33+
34+
while not l1.head.isNil:
35+
temp_la.add(l1.head.value)
36+
l1.head = l1.head.next
37+
38+
while not l2.head.isNil:
39+
temp_lb.add(l2.head.value)
40+
l2.head = l2.head.next
41+
42+
psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt()))
43+
for i in psum: aggregate.append(($i).parseInt())
44+
45+
result = aggregate
46+
47+
var list1: SinglyLinkedList[int]
48+
var list2: SinglyLinkedList[int]
49+
50+
for i in @[2, 4, 3]: list1.append(i)
51+
for i in @[5, 6, 4]: list2.append(i)
52+
53+
echo(preview(list1))
54+
echo(preview(list2))
55+
echo(preview(addTwoNumbers(list1, list2)))
56+

0 commit comments

Comments
 (0)