Tweaked add reverse challenge.

This commit is contained in:
Donne Martin 2015-07-05 09:48:05 -04:00
parent e75e128aa5
commit 0adf6c4be0
2 changed files with 15 additions and 15 deletions

View File

@ -38,8 +38,8 @@
"\n", "\n",
"* Do you expect the return to be in reverse order too?\n", "* Do you expect the return to be in reverse order too?\n",
" * Yes\n", " * Yes\n",
"* What if one of the inputs is NULL?\n", "* What if one of the inputs is None?\n",
" * Return NULL for an invalid operation\n", " * Return None for an invalid operation\n",
"* How large are these numbers--can they fit in memory?\n", "* How large are these numbers--can they fit in memory?\n",
" * Yes\n", " * Yes\n",
"* Can we assume we already have a linked list class that can be used for this problem?\n", "* Can we assume we already have a linked list class that can be used for this problem?\n",

View File

@ -37,8 +37,8 @@
"\n", "\n",
"* Do you expect the return to be in reverse order too?\n", "* Do you expect the return to be in reverse order too?\n",
" * Yes\n", " * Yes\n",
"* What if one of the inputs is NULL?\n", "* What if one of the inputs is None?\n",
" * Return NULL for an invalid operation\n", " * Return None for an invalid operation\n",
"* How large are these numbers--can they fit in memory?\n", "* How large are these numbers--can they fit in memory?\n",
" * Yes\n", " * Yes\n",
"* Can we assume we already have a linked list class that can be used for this problem?\n", "* Can we assume we already have a linked list class that can be used for this problem?\n",
@ -73,17 +73,17 @@
" * if first and second lists are NULL AND carry is zero\n", " * if first and second lists are NULL AND carry is zero\n",
" * Return NULL\n", " * Return NULL\n",
"* Recursive case:\n", "* Recursive case:\n",
" * value = carry\n", " * Set `value` to `carry`\n",
" * value += first.data + second.data\n", " * Add both nodes' `data` to `value`\n",
" * remainder = value % 10\n", " * Set the `new_carry` to 1 if `value >= 10, else 0`\n",
" * new_carry = 1 if value >= 10, else 0\n", " * Set the `remainder` to `value % 10`\n",
" * Create a node with the remainder\n", " * Create a `node` with the `remainder`\n",
" * node.next = self.add(first.next, second.next, new_carry)\n", " * Set `node.next` to a recursive call on the `next` nodes, passing in the `carry`\n",
" * Return node\n", " * Return `node`\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
"* Space: O(n), extra space for result and recursion depth\n", "* Space: O(m), extra space for result and recursion depth\n",
"\n", "\n",
"Notes:\n", "Notes:\n",
"* Careful with adding if the lists differ\n", "* Careful with adding if the lists differ\n",
@ -131,12 +131,12 @@
" value = carry\n", " value = carry\n",
" value += first_node.data if first_node is not None else 0\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", " 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", " new_carry = 1 if value >= 10 else 0\n",
" remainder = value % 10\n",
" node = Node(remainder)\n", " node = Node(remainder)\n",
" node.next = self.__add_reverse__(first_node.next if first_node is not None else None, \n", " node.next = self.__add_reverse__(first_node.next if first_node is not None else None, \n",
" second_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", " new_carry)\n",
" return node\n", " return node\n",
"\n", "\n",
" def add_reverse(self, first_list, second_list):\n", " def add_reverse(self, first_list, second_list):\n",