|
| 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 | +} |
0 commit comments