mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish delete mid node challenge and solution
Update edge cases and tests.
This commit is contained in:
parent
99239dda62
commit
37cf2028e6
|
@ -170,21 +170,21 @@
|
|||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 2",
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python2"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 2
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython2",
|
||||
"version": "2.7.10"
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -96,9 +96,10 @@
|
|||
"class MyLinkedList(LinkedList):\n",
|
||||
"\n",
|
||||
" def delete_node(self, node):\n",
|
||||
" if self.head is None:\n",
|
||||
" if self.head is None or node is None:\n",
|
||||
" return\n",
|
||||
" if node is None:\n",
|
||||
" if self.head == node:\n",
|
||||
" self.head = None\n",
|
||||
" return\n",
|
||||
" if node.next is None:\n",
|
||||
" node.data = None\n",
|
||||
|
@ -147,16 +148,25 @@
|
|||
" head = Node(2)\n",
|
||||
" linked_list = MyLinkedList(head)\n",
|
||||
" linked_list.delete_node(head)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [None])\n",
|
||||
" assert_equal(linked_list.get_all_data(), [])\n",
|
||||
"\n",
|
||||
" print('Test: Multiple nodes')\n",
|
||||
" linked_list = MyLinkedList(None)\n",
|
||||
" node0 = linked_list.insert_to_front(1)\n",
|
||||
" node0 = linked_list.insert_to_front(2)\n",
|
||||
" node1 = linked_list.insert_to_front(3)\n",
|
||||
" node2 = linked_list.insert_to_front(4)\n",
|
||||
" node3 = linked_list.insert_to_front(1)\n",
|
||||
" linked_list.delete_node(node2)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [1, 3, 1])\n",
|
||||
" linked_list.delete_node(node1)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [1, 4, 2])\n",
|
||||
"\n",
|
||||
" print('Test: Multiple nodes, delete last element')\n",
|
||||
" linked_list = MyLinkedList(None)\n",
|
||||
" node0 = linked_list.insert_to_front(2)\n",
|
||||
" node1 = linked_list.insert_to_front(3)\n",
|
||||
" node2 = linked_list.insert_to_front(4)\n",
|
||||
" node3 = linked_list.insert_to_front(1)\n",
|
||||
" linked_list.delete_node(node0)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [1, 4, 3, None])\n",
|
||||
"\n",
|
||||
" print('Success: test_delete_node')\n",
|
||||
"\n",
|
||||
|
@ -184,6 +194,7 @@
|
|||
"Test: Empty list, null node to delete\n",
|
||||
"Test: One node\n",
|
||||
"Test: Multiple nodes\n",
|
||||
"Test: Multiple nodes, delete last element\n",
|
||||
"Success: test_delete_node\n"
|
||||
]
|
||||
}
|
||||
|
@ -209,7 +220,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.4.3"
|
||||
"version": "3.5.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
|
|
@ -13,16 +13,25 @@ class TestDeleteNode(object):
|
|||
head = Node(2)
|
||||
linked_list = MyLinkedList(head)
|
||||
linked_list.delete_node(head)
|
||||
assert_equal(linked_list.get_all_data(), [None])
|
||||
assert_equal(linked_list.get_all_data(), [])
|
||||
|
||||
print('Test: Multiple nodes')
|
||||
linked_list = MyLinkedList(None)
|
||||
node0 = linked_list.insert_to_front(1)
|
||||
node0 = linked_list.insert_to_front(2)
|
||||
node1 = linked_list.insert_to_front(3)
|
||||
node2 = linked_list.insert_to_front(4)
|
||||
node3 = linked_list.insert_to_front(1)
|
||||
linked_list.delete_node(node2)
|
||||
assert_equal(linked_list.get_all_data(), [1, 3, 1])
|
||||
linked_list.delete_node(node1)
|
||||
assert_equal(linked_list.get_all_data(), [1, 4, 2])
|
||||
|
||||
print('Test: Multiple nodes, delete last element')
|
||||
linked_list = MyLinkedList(None)
|
||||
node0 = linked_list.insert_to_front(2)
|
||||
node1 = linked_list.insert_to_front(3)
|
||||
node2 = linked_list.insert_to_front(4)
|
||||
node3 = linked_list.insert_to_front(1)
|
||||
linked_list.delete_node(node0)
|
||||
assert_equal(linked_list.get_all_data(), [1, 4, 3, None])
|
||||
|
||||
print('Success: test_delete_node')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user