# Kotlin Algorithm Challenge #1

The following problem is the Add Two Numbers problem on LeetCode.

## Problem

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

For example:

### Linked List

Weâ€™ll use the following class as the basis for our linked list.

## Collapse Function

In order to add the 2 linked lists, weâ€™ll convert them to numbers, then add them, then convert the result back to a linked list.

As a first step, letâ€™s create a function that takes in a ListNode, and returns the integer it represents.

The goal here is to iterate through the list, maintaining a sum of the final number. With each iteration, we add the value of the current node (multiplied by a factor of 10) to the running total. The factor of 10 is just 10 to the power of itâ€™s place in the list.

Note that weâ€™re iterating through a linked list, so weâ€™ll do that with a `while`

loop until the `node.next = null`

.

Letâ€™s check if our function works as imagined. Recall that the numbers in the linked list are the starting at the least significant digits.

## Expand Function

Conversely, letâ€™s write a function that takes in an integer and returns a `ListNode`

. Thisâ€™ll be useful because the final result is supposed to be a `ListNode`

. Also it helps us with testing.

As with the `collapse`

function, there are two main approaches we could take to go to and from the linked list to an integer.

The easiest data structure to handle this problem is an list of the digits. In order to get there, we could take the numeric route using powers of 10 like we did in the `collapse`

function, but itâ€™s simpler to just use string parsing.

Once we have a list of the digits, then we just need to reduce the list to a single list node. For this, weâ€™re going to use the Kotlin `fold`

function.

`fold`

is very similar to `reduce`

, but reduce takes the first value weâ€™re iterating over as the initial value, whereas with `fold`

we can specify our own. In our case this is important, because we want the final value to be a `ListNode`

, not an `Int`

.

So, weâ€™ll create a list node with the first digit and use it as our initial value, e.g. `ListNode(digits.get(0), null)`

.

Awesome.

# A Better Solution

Gonna be honest, the first time I did this problem, the above was my solution. But 30 minutes after I published this, it was gnawing at me that I had to go collapse and expand the lists, going through them multiple times.

A few things made me think I was on the wrong path:

- They want you to return a linked list. Why would I go to a number, then re-expand to a linked list?
- Thereâ€™s really no reason you canâ€™t traverse both at the same time, although you do have to maintain state because the sum of two digits can be greater than 9, meaning weâ€™d carry over to the next digit.

Hereâ€™s my final solution; itâ€™s simpler, but a little tougher to follow. Letâ€™s create a recursive helper function, that takes in 2 nodes, and adds them together, and allows us to pass state through in a function parameter.

This function is much cleanerâ€”thereâ€™s no while loop. The key things that might be confusing are Kotlinâ€™s null safety and the elvis operator.

Anyway, once we have this helper function, we get a `O(log_10(x + y))`

solution.

# Summary

This was an interesting problem to solve. As you guys see, my first inclination is technically correct, but isnâ€™t the most efficient or concise way of going about it. I had to let it marinate for a while before I actually got the succinct recursive solution.

Thatâ€™s how these problems go a lot of the time.

Wow! You read the whole thing. People who make it this far sometimes
want to receive emails when I post something new.

I also have an RSS feed.