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.