MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | feat/linked list - #4
Open
marissamolejon wants to merge 1 commit into
Open
MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 2 | feat/linked list#4marissamolejon wants to merge 1 commit into
marissamolejon wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements
LinkedListfrom scratch inlinked_list.py, supportingpush_head,pop_tail, andremove, each with O(1) worst-case timecomplexity as required.
Changes made
Nodeclass holdingvalue,previous, andnext.LinkedListclass holdingheadandtail.push_head(value)— inserts a new node at the front, updateshead(and
tail, if the list was empty), and returns the node itself as ahandle for later removal.
pop_tail()— reads the tail's value, removes the tail node viaremove, and returns the value.remove(node)— reconnectsnode.previousandnode.nextaround theremoved node, updating
head/tailwhen the removed node was ateither end, and clears the removed node's own pointers.
Design notes
The list is doubly-linked (each node stores both
previousandnext), which is what makesremove(node)O(1): given a directreference to a node, its neighbors can be reconnected without searching
the rest of the list. A singly-linked list would require an O(n) walk
from
headto find the node's predecessor.pop_tailis implemented in terms ofremove, rather than duplicatingthe pointer-reconnection logic, so there's a single source of truth for
"how to detach a node from the list."
Testing
All three provided tests pass:
test_pushes_then_pops— confirmspush_head/pop_tailordertest_remove— confirms removing a middle node reconnects its neighborstest_remove_tail— confirms head/tail updates and pointer cleanup onthe removed node
Ran locally via
python3 -m pytest linked_list_test.py -v— all passing.Learning points
removegiven a node handle: the node itselfmust carry enough information (
previousandnext) to fix up itsneighbors without any search. This is the core trade-off a
doubly-linked list makes over a singly-linked one — a bit more memory
per node, in exchange for O(1) removal from anywhere in the list.
removeinsidepop_tailavoids duplicating theneighbor-reconnection logic in two places.
Checklist