Skip to content

London | 26-Jul-SDC | Roman Sanaye | Sprint 2 | Implement a linked list in Python - #242

Open
RomanSanaye wants to merge 3 commits into
CodeYourFuture:mainfrom
RomanSanaye:implement-linked-list-in-python
Open

RomanSanaye wants to merge 3 commits into
CodeYourFuture:mainfrom
RomanSanaye:implement-linked-list-in-python

Conversation

@RomanSanaye

Copy link
Copy Markdown

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

Task code

CYF-1180

Changelist

  • Implemented a Doubly Linked List in linked_list.py using Node and LinkedList classes.
  • Ensured $O(1)$ worst-case time complexity for all core operations:
    • push_head: Adds an element to the start and returns the node as a direct handle.
    • pop_tail: Removes an element from the back using the tail pointer instantly.
    • remove: Removes a specific node in constant time using its handle.
  • Handled all boundary scenarios (head, tail, middle, and single-element lists) to ensure memory safety.
  • Verified and successfully passed all unit tests in linked_list_test.py.

Questions

No questions at the moment—all unit tests passed successfully!

@RomanSanaye RomanSanaye added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 12, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could consider adding type hints to each method to clearly specify the types of its parameters and return value.

Comment on lines +3 to +7
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.previous = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

May I suggest exploring the use of __slots__ to reduce memory usage?

Comment on lines +44 to +51
if self.head == self.tail:
self.head = None
self.tail = None
else:
# Move the tail pointer back to the previous node
self.tail = self.tail.previous
# Cut off the old tail by clearing its 'next' reference
self.tail.next = None

Copy link
Copy Markdown

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.

@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 Oct 13, 2026
@RomanSanaye

Copy link
Copy Markdown
Author

Hi @cjyuan ,
Thanks for all the great feedback! I've updated the code with the following improvements:

  1. Added clear type hints to all methods, parameters, and return values.
  2. Optimized Node memory usage using __slots__.
  3. Refactored pop_tail() to delegate directly to remove() to keep the implementation DRY.

Ready for re-review!

@RomanSanaye RomanSanaye added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 15, 2026

@cjyuan cjyuan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Changes look good. Well done.

self.next = None
self.previous = None
__slots__ = ("value", "next", "previous")
def __init__(self, value: Any)-> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Additional reading: Use AI to explore

  • Specifying type as object vs as Any
  • Python Generics

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants