#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Delete a node in the middle, given only access to that node. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Can we assume this is a non-circular, singly linked list? # * Yes # * What if the final node is being deleted, do we make it a dummy with value None? # * Yes # * Can we assume we already have a linked list class that can be used for this problem? # * Yes # ## Test Cases # # * Delete on empty list -> None # * Delete None -> None # * Delete on one node -> [None] # * Delete on multiple nodes # ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/linked_lists/delete_mid/delete_mid_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start. # ## Code # In[ ]: get_ipython().run_line_magic('run', '../linked_list/linked_list.py') get_ipython().run_line_magic('load', '../linked_list/linked_list.py') # In[ ]: class MyLinkedList(LinkedList): def delete_node(self, node): # TODO: Implement me pass # ## Unit Test # # # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_delete_mid.py import unittest class TestDeleteNode(unittest.TestCase): def test_delete_node(self): print('Test: Empty list, null node to delete') linked_list = MyLinkedList(None) linked_list.delete_node(None) self.assertEqual(linked_list.get_all_data(), []) print('Test: One node') head = Node(2) linked_list = MyLinkedList(head) linked_list.delete_node(head) self.assertEqual(linked_list.get_all_data(), [None]) print('Test: Multiple nodes') 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(node1) self.assertEqual(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) self.assertEqual(linked_list.get_all_data(), [1, 4, 3, None]) print('Success: test_delete_node') def main(): test = TestDeleteNode() test.test_delete_node() if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/linked_lists/delete_mid/delete_mid_solution.ipynb) for a discussion on algorithms and code solutions.