mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-11 11:58:02 +00:00
Polish delete mid node challenge and solution
Update edge cases and tests.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user