Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 2 | Implement a linked list in Python#125
Conversation
…kenly commited into main and thus my PR failed.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
20653de to
e862c3b
Compare
cjyuan
left a comment
There was a problem hiding this comment.
The code in linked_list_test.py expects both .next and .previous properties of the removed node to be assigned None. Currently your implementation could not pass the tests.
Note: Do you know the why it is a good practice to assign .next and .previous of the removed node to None?
|
The original test is not quite complete. I added a few more myself, and here is one of them. def test_remove_middle(self):
l = LinkedList()
l.push_head("a")
b = l.push_head("b")
l.push_head("c")
l.remove(b)
self.assertIsNone(b.next)
self.assertIsNone(b.previous)
self.assertEqual(l.pop_tail(), "a")
self.assertEqual(l.pop_tail(), "c") |
Thanks for sharing, I will update my test file to include what've just shared and then work to pass it. |
Is it to ensure that the removed node is dormant/permanently isolated, so that nobody accidentally in the future re-link it back to the list? |
That one of the reasons. Another one is related to garbage collection. |
…ty and thus throw error to eliminate having to deal with special cases after this point
…st, reposition the rest of the code at the end
…to ensure that it's fully isolated/disconnected
|
Currently it is still possible that some removed nodes do not have their Can you fix the bug? |
…efore removal/relinking to avoid possible bugs
|
updated my logic now so we first capture the previous and next nodes to our current node before any changes we make to the linked array structure (re-linking). |
|
Currently it is still possible that some removed nodes do not have their This is part of the cons of "having more code to maintain" -- focus on where node can be removed from the linked list in your implementation. |
… pointing to None. This is to ensure the removed node is fully isolated and dromant.
My bad, missed doing the same process inside pop_tail function. Anyway, it's all sorted now. Thanks CJ :) |
| tail_node = self.tail | ||
| previous = self.tail.previous | ||
|
|
||
| self.tail = previous | ||
| if self.tail is not None: | ||
| self.tail.next = None | ||
| else: | ||
| self.head = None | ||
|
|
||
| tail_node.previous = None | ||
| tail_node.next = None | ||
|
|
||
| return tail_node.data |
There was a problem hiding this comment.
-
Is line 40 needed?
-
You could replace the code on lines 30-42 by 3 lines of code if you delegate the node-removing task to
remove().

Self checklist
In this pull request, I implemented a linked list with push_head, pop_tail, and remove operations, all in O(1) time.