mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-10 03:18:02 +00:00
Update LL delete mid challenge (#112)
Update constraints and assumptions, solution, and test case.
This commit is contained in:
@@ -36,7 +36,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"* Can we assume this is a non-circular, singly linked list?\n",
|
"* Can we assume this is a non-circular, singly linked list?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* What if the final node is being deleted, for example a single node list? Do we make it a dummy with value None?\n",
|
"* What if the final node is being deleted, do we make it a dummy with value None?\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",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
@@ -141,12 +141,21 @@
|
|||||||
"\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",
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"* Can we assume this is a non-circular, singly linked list?\n",
|
"* Can we assume this is a non-circular, singly linked list?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* What if the final node is being deleted, for example a single node list? Do we make it a dummy with value None?\n",
|
"* What if the final node is being deleted, do we make it a dummy with value None?\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",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
@@ -98,14 +98,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 or node is None:\n",
|
" if node is None:\n",
|
||||||
" return\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",
|
||||||
" node.next = None\n",
|
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" node.data = node.next.data\n",
|
" node.data = node.next.data\n",
|
||||||
" node.next = node.next.next"
|
" node.next = node.next.next"
|
||||||
@@ -150,7 +146,7 @@
|
|||||||
" 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(), [])\n",
|
" assert_equal(linked_list.get_all_data(), [None])\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Test: Multiple nodes')\n",
|
" print('Test: Multiple nodes')\n",
|
||||||
" linked_list = MyLinkedList(None)\n",
|
" linked_list = MyLinkedList(None)\n",
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ 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(), [])
|
assert_equal(linked_list.get_all_data(), [None])
|
||||||
|
|
||||||
print('Test: Multiple nodes')
|
print('Test: Multiple nodes')
|
||||||
linked_list = MyLinkedList(None)
|
linked_list = MyLinkedList(None)
|
||||||
|
|||||||
Reference in New Issue
Block a user