2015-05-14 18:59:38 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Add two numbers whose digits are stored in a linked list in reverse order.\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
"* Do you expect the return to be in reverse order too?\n",
" * Yes\n",
"* What if one of the inputs is NULL?\n",
" * Return NULL for an invalid operation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* Empty list(s)\n",
"* Add values of different lengths\n",
" * Input 1: 6->5->None\n",
" * Input 2: 9->8->7\n",
" * Result: 5->4->8\n",
"* Add values of same lengths\n",
" * Exercised from values of different lengths\n",
" * Done here for completeness"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
2015-05-14 19:03:05 +08:00
"We could solve this with an iterative or a recursive algorithm, both are well suited for this exercise. We'll use a recursive algorithm for practice with recursion. Note this takes an extra space of O(m) where m is the recursion depth.\n",
2015-05-14 18:59:38 +08:00
"\n",
"* Test for error cases\n",
"* Careful with adding if the lists differ\n",
" * Only add if a node is not NULL\n",
" * Alternatively, we could add trailing zeroes to the smaller list\n",
"* Base case:\n",
" * if first and second lists are NULL AND carry is zero\n",
" * Return NULL\n",
"* Recursive case:\n",
" * value = carry\n",
" * value += first.data + second.data\n",
" * remainder = value % 10\n",
" * new_carry = 1 if value >= 10, else 0\n",
" * Create a node with the remainder\n",
" * node.next = self.add(first.next, second.next, new_carry)\n",
" * Return node\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(n), extra space for result and recursion depth"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run linked_list.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class MyLinkedList(LinkedList):\n",
" def __add__(self, first_node, second_node, carry):\n",
" if type(carry) != int and carry < 0:\n",
" raise ValueError('Invalid int argument: carry')\n",
" if first_node is None and second_node is None and carry == 0:\n",
" return None\n",
" value = carry\n",
" value += first_node.data if first_node is not None else 0\n",
" value += second_node.data if second_node is not None else 0\n",
" remainder = value % 10\n",
" new_carry = 1 if value >= 10 else 0\n",
" node = Node(remainder)\n",
" #import pdb; pdb.set_trace()\n",
" node.next = self.__add__(first_node.next if first_node is not None else None, \n",
" second_node.next if first_node is not None else None, \n",
" new_carry)\n",
" return node\n",
"\n",
" def add(self, first_list, second_list):\n",
" if first_list is None or second_list is None:\n",
" return None\n",
" head = self.__add__(first_list.head, second_list.head, 0)\n",
" return MyLinkedList(head)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Add values of different lengths')\n",
"# Input 1: 6->5->None\n",
"# Input 2: 9->8->7\n",
"# Result: 5->4->8\n",
"first_head = Node(6)\n",
"first_list = MyLinkedList(first_head)\n",
"first_list.append(5)\n",
"second_head = Node(9)\n",
"second_list = MyLinkedList(second_head)\n",
"second_list.append(8)\n",
"second_list.append(7)\n",
"result = MyLinkedList().add(first_list, second_list)\n",
"result.print_list()\n",
"print('Add values of same lengths')\n",
"# Input 1: 6->5->4\n",
"# Input 2: 9->8->7\n",
"# Result: 5->4->2->1\n",
"first_head = Node(6)\n",
"first_list = MyLinkedList(first_head)\n",
"first_list.append(5)\n",
"first_list.append(4)\n",
"second_head = Node(9)\n",
"second_list = MyLinkedList(second_head)\n",
"second_list.append(8)\n",
"second_list.append(7)\n",
"result = MyLinkedList().add(first_list, second_list)\n",
"result.print_list()\n",
"print('Empty list(s)')\n",
"result = MyLinkedList().add(None, None)\n",
"result = MyLinkedList().add(first_head, None)\n",
"result = MyLinkedList().add(None, second_head)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}