Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | Implement a linked list in Python#121
Conversation
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.
69d651d to
602505d
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 all the tests.
Note: Do you know the why it is a good practice to assign .next and .previous of the removed node to None?
| if self.head == self.tail: | ||
| self.head = None | ||
| self.tail = None | ||
| else: | ||
| self.tail = self.tail.previous | ||
| self.tail.next = None |
There was a problem hiding this comment.
Could consider delegating this task to remove() -- less code to maintain.
|
@cjyuan I appreciate the explanation about why it is a good practice to assign .next and .previous good practice—it makes sense for memory management, preventing accidental traversal, and making debugging easier. |
|
The code in The tests in 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 the feedback @cjyuan ! I've updated the implementation to set .next and .previous to None after removal. |
|
I don't see any new commits on this PR branch. |
|
Sorry @cjyuan I pushed it now |
|
There is still only 1 commit on this PR branch. You have two "linked list" branches on your repo. Did you push your change on this branch? |
|
Sorry @cjyuan for the confusion now the changes are updated |
| current = current.next | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"LinkedList([{', '.join(str(v) for v in self)}])" No newline at end of file |
There was a problem hiding this comment.
ChatGPT suggests this:
Using
repr(v)is the conventional approach inside__repr__.
Seems reasonable but I did not verify it.
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | Implement a linked list in Python
Learners, PR Template
Self checklist
Implement a Doubly-Linked List in Python
Description
Implemented a complete doubly-linked list data structure with O(1) worst-case time complexity for all required operations.
Changes
Nodeclass with pointers to next and previous nodesLinkedListclass with efficient head/tail managementPlease review my work — thank you!