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