Skip to content

Commit d7fc448

Browse files
authored
Add Add Two Numbers (#131)
1 parent 2e108b8 commit d7fc448

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ multiple times and be persistent over time.
114114
- [Radix sort](src/test/kotlin/com/igorwojda/list/sort/radixsort)
115115
- [Doubly linked list](src/test/kotlin/com/igorwojda/linkedlist/doubly/base)
116116
- [Max binary heap](src/test/kotlin/com/igorwojda/tree/heap/maxbinaryheap)
117+
- [Add Numbers](src/test/kotlin/com/igorwojda/linkedlist/singly/addnumbers)
117118

118119
**Expert**
119120

misc/ChallengeGroups.md

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ We use sliding window instead of nested loops which decreases complexity from `O
101101
- [Circular linked list?](../src/test/kotlin/com/igorwojda/linkedlist/singly/circularcheck/README.md)
102102
- [Take n-th element from last](../src/test/kotlin/com/igorwojda/linkedlist/singly/fromlast/README.md)
103103
- [Midpoint](../src/test/kotlin/com/igorwojda/linkedlist/singly/midpoint/README.md)
104+
- [Add Numbers](../src/test/kotlin/com/igorwojda/linkedlist/singly/addnumbers)
104105

105106
## Doubly Linked List
106107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.igorwojda.linkedlist.singly.addnumbers
2+
3+
import org.amshove.kluent.shouldBeEqualTo
4+
import org.junit.jupiter.api.Test
5+
6+
private data class ListNode(
7+
var data: Int,
8+
var next: ListNode? = null,
9+
)
10+
11+
private fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
12+
TODO("Add your solution here")
13+
}
14+
15+
private class Test {
16+
@Test
17+
fun `add 5, 3, 7 to 2, 3, 3 returns 7, 3, 8`() {
18+
val number1 = getList(5, 3, 7)
19+
val number2 = getList(7, 3, 8)
20+
val result = getList(2, 7, 5, 1)
21+
22+
addTwoNumbers(number1, number2) shouldBeEqualTo result
23+
}
24+
25+
@Test
26+
fun `add 0 to 0 returns 0`() {
27+
val number1 = getList(0)
28+
val number2 = getList(0)
29+
val result = getList(0)
30+
31+
addTwoNumbers(number1, number2) shouldBeEqualTo result
32+
}
33+
34+
@Test
35+
fun `add 7 to 2, 3, 5 returns 9, 3, 5`() {
36+
val number1 = getList(7)
37+
val number2 = getList(2, 3, 5)
38+
val result = getList(9, 3, 5)
39+
40+
addTwoNumbers(number1, number2) shouldBeEqualTo result
41+
}
42+
43+
private fun getList(vararg ints: Int): ListNode? {
44+
var head: ListNode? = null
45+
var current: ListNode? = null
46+
47+
ints.forEach {
48+
val node = ListNode(it)
49+
50+
if (head == null) {
51+
head = node
52+
current = node
53+
} else {
54+
current?.next = node
55+
current = node
56+
}
57+
}
58+
59+
return head
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add Numbers
2+
3+
## Instructions
4+
5+
Compute the sum of two non-empty linked lists, each representing a non-negative integer. he individual digits of these
6+
integers are stored in the linked lists in reverse order, with each node holding a single digit. The result of the
7+
addition should be returned as a linked list.
8+
9+
[Challenge](Challenge.kt) | [Solution](Solution.kt)
10+
11+
## Examples
12+
13+
```kotlin
14+
private data class ListNode(
15+
var data: Int,
16+
var next: ListNode? = null,
17+
)
18+
19+
val i1 = ListNode(1)
20+
i1.next = ListNode(3)
21+
22+
val i2 = ListNode(11)
23+
24+
addTwoNumbers(i1, i2) // returns ListNode(2, ListNode(4))
25+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.igorwojda.linkedlist.singly.addnumbers
2+
3+
object Solution1 {
4+
private fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
5+
val n1 = getNumber(l1)
6+
val n2 = getNumber(l2)
7+
8+
val result = n1 + n2
9+
10+
return getList(result)
11+
}
12+
13+
private fun getNumber(l1: ListNode?): Int {
14+
var node = l1
15+
var numberStr = ""
16+
17+
while (node != null) {
18+
numberStr += node.data
19+
node = node.next
20+
}
21+
22+
return numberStr.reversed().toInt()
23+
}
24+
25+
private fun getList(int: Int): ListNode? {
26+
val intStr = int.toString().reversed()
27+
var list: ListNode? = null
28+
var lastListNode: ListNode? = null
29+
30+
intStr.forEach {
31+
val node = ListNode(it.digitToInt())
32+
33+
if (list == null) {
34+
list = node
35+
lastListNode = node
36+
} else {
37+
lastListNode?.next = node
38+
lastListNode = node
39+
}
40+
}
41+
42+
return list
43+
}
44+
45+
private data class ListNode(
46+
var data: Int,
47+
var next: ListNode? = null,
48+
)
49+
}
50+
51+
private object KtLintWillNotComplain

0 commit comments

Comments
 (0)