mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Tweaked add reverse challenge.
This commit is contained in:
parent
e75e128aa5
commit
0adf6c4be0
|
@ -38,8 +38,8 @@
|
|||
"\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\n",
|
||||
"* What if one of the inputs is None?\n",
|
||||
" * Return None for an invalid operation\n",
|
||||
"* How large are these numbers--can they fit in memory?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||
|
|
|
@ -37,8 +37,8 @@
|
|||
"\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\n",
|
||||
"* What if one of the inputs is None?\n",
|
||||
" * Return None for an invalid operation\n",
|
||||
"* How large are these numbers--can they fit in memory?\n",
|
||||
" * Yes\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",
|
||||
" * 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",
|
||||
" * Set `value` to `carry`\n",
|
||||
" * Add both nodes' `data` to `value`\n",
|
||||
" * Set the `new_carry` to 1 if `value >= 10, else 0`\n",
|
||||
" * Set the `remainder` to `value % 10`\n",
|
||||
" * Create a `node` with the `remainder`\n",
|
||||
" * Set `node.next` to a recursive call on the `next` nodes, passing in the `carry`\n",
|
||||
" * Return `node`\n",
|
||||
"\n",
|
||||
"Complexity:\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",
|
||||
"Notes:\n",
|
||||
"* Careful with adding if the lists differ\n",
|
||||
|
@ -131,12 +131,12 @@
|
|||
" 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",
|
||||
" remainder = value % 10\n",
|
||||
" node = Node(remainder)\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",
|
||||
" new_carry)\n",
|
||||
" second_node.next if first_node is not None else None, \n",
|
||||
" new_carry)\n",
|
||||
" return node\n",
|
||||
"\n",
|
||||
" def add_reverse(self, first_list, second_list):\n",
|
||||
|
|
Loading…
Reference in New Issue
Block a user