Introduction
It is the algorithm related to linked list. I like to learn how to write a recursive solution, since I like to write a short solution.
Here is the code I read from one of blogs, I like to try it later.
Recursive and iterative solutions
I like to write the code to handle a few test cases in the following order:
1. One of lists is empty;
2. Add one node list together, no carry;
3. Make it work if there is carry, then I need to call the recursive function twice instead of once.
4. Think about how to extend the linked list to more than one node. Add recursive call function.
Here is C# code I wrote using recursive function.
I also spent 10 minutes to review iterative solution written in 2015, and I reviewed the code.
Version of code: 2015 iterative, link is here.
Version of code: 2018 iterative, link is here.
Actionable Items
One way to train myself to write a quick recursive solution is to write a prototype one first. Here is the one I wrote to show how to do it.
Based on prototype function, I just need to add the linked list with more nodes.
I did not think cleary last night since the prototype is more complicated than copy linked list with a random pointer algorithm. Since the prototype already involves one recursive function call.
No comments:
Post a Comment