Skip to content

Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 2 | Implement a linked list in Python#125

Open
HassanOHOsman wants to merge 6 commits intoCodeYourFuture:mainfrom
HassanOHOsman:feature/implement-linked-list-in-python
Open

Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 2 | Implement a linked list in Python#125
HassanOHOsman wants to merge 6 commits intoCodeYourFuture:mainfrom
HassanOHOsman:feature/implement-linked-list-in-python

Conversation

@HassanOHOsman
Copy link

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

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

…kenly commited into main and thus my PR failed.
@HassanOHOsman HassanOHOsman added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 1 Assigned during Sprint 1 of this module 📅 Sprint 2 Assigned during Sprint 2 of this module and removed 📅 Sprint 1 Assigned during Sprint 1 of this module labels Feb 27, 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 27, 2026
@github-actions

This comment has been minimized.

@HassanOHOsman HassanOHOsman added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 27, 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 27, 2026
@HassanOHOsman HassanOHOsman added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed 📅 Sprint 2 Assigned during Sprint 2 of this module labels Feb 27, 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 27, 2026
@github-actions

This comment has been minimized.

@HassanOHOsman HassanOHOsman added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 2 Assigned during Sprint 2 of this module labels Feb 27, 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 27, 2026
@github-actions

This comment has been minimized.

@HassanOHOsman HassanOHOsman force-pushed the feature/implement-linked-list-in-python branch from 20653de to e862c3b Compare February 27, 2026 07:50
@HassanOHOsman HassanOHOsman added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 27, 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 the tests.

Note: Do you know the why it is a good practice to assign .next and .previous of the removed node to None?

self.head = None
else:
raise IndexError("Unable to remove from empty linked list")

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 the node removing task to remove() -- less code to maintain.

  • Could also consider this style of code writing:

        # Handle all errors and special cases first
        if self.tail is None:
            raise IndexError("Unable to remove from empty linked list")

        # From this point onward, we don't have to worry about special cases.

        # No else needed; one less indentation level.
        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

@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
@HassanOHOsman
Copy link
Author

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?

Strangely, all three tests passed on my end. Here's a screenshot for you reference.
Screenshot 2026-02-27 at 14 41 20

Having said that, I will check it again and see what's gone wrong. Thank you CJ :)

@cjyuan
Copy link

cjyuan commented Feb 27, 2026

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

@HassanOHOsman
Copy link
Author

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.

@HassanOHOsman
Copy link
Author

Note: Do you know the why it is a good practice to assign .next and .previous of the removed node to None?

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?

@cjyuan
Copy link

cjyuan commented Feb 27, 2026

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
@HassanOHOsman HassanOHOsman added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Feb 27, 2026
@cjyuan
Copy link

cjyuan commented Feb 27, 2026

Currently it is still possible that some removed nodes do not have their .previous properly set to None.

Can you fix the bug?

@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
…efore removal/relinking to avoid possible bugs
@HassanOHOsman HassanOHOsman added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Feb 27, 2026
@HassanOHOsman
Copy link
Author

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

@cjyuan
Copy link

cjyuan commented Feb 28, 2026

Currently it is still possible that some removed nodes do not have their .previous properly set to None.

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.

@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 28, 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. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants