Skip to content

Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | Implement a linked list in Python#121

Open
RahwaZeslusHaile wants to merge 1 commit intoCodeYourFuture:mainfrom
RahwaZeslusHaile:Implement-a-linked-list-in-Python
Open

Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | Implement a linked list in Python#121
RahwaZeslusHaile wants to merge 1 commit intoCodeYourFuture:mainfrom
RahwaZeslusHaile:Implement-a-linked-list-in-Python

Conversation

@RahwaZeslusHaile
Copy link

Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | Implement a linked list in Python

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

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

  • Created Node class with pointers to next and previous nodes
  • Created LinkedList class with efficient head/tail management
  • Implemented three core operations with guaranteed O(1) performance:
    • push_head(): Insert element at list head, returns handle for later removal
    • pop_tail(): Remove and return element from list end
    • remove(): Remove any element given its handle, without list traversal

Please review my work — thank you!

@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 25, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 25, 2026
@github-actions

This comment has been minimized.

@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 25, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 25, 2026
@RahwaZeslusHaile RahwaZeslusHaile force-pushed the Implement-a-linked-list-in-Python branch from 69d651d to 602505d Compare February 25, 2026 23:27
@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 25, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines +33 to +38
if self.head == self.tail:
self.head = None
self.tail = None
else:
self.tail = self.tail.previous
self.tail.next = None
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider delegating this task to remove() -- less code to maintain.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Feb 27, 2026
@RahwaZeslusHaile
Copy link
Author

@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.

@RahwaZeslusHaile RahwaZeslusHaile added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 27, 2026
@cjyuan
Copy link

cjyuan commented Feb 27, 2026

The code in linked_list_test.py expects both .next and .previous properties of the removed node to be assigned None.
Can you ensure your implementation meet this expectation?

The tests in linked_list_test.py is not quite complete. Here' is a test I added, which your implementation could not pass:

    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")

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants