Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Optional


class Node:
__slots__ = ("value", "next", "previous")

def __init__(self, value: str) -> None:

@cjyuan cjyuan Oct 14, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Limiting the value to be of type str would make the linked list less flexible -- could not be reused for different typesof data.

Suggestion: Explore the topic "Generics" in Python. You could ask AI this:

In Python, how to ensure a parameter (and its storage) maintains a consistent type $T$ throughout a class?

Note: Python 3.12+ introduce a new syntax which make specifying generic type easier.

No change needed

self.value: str = value
self.next: Optional[Node] = None
self.previous: Optional[Node] = None


class LinkedList:
def __init__(self) -> None:
self.head: Optional[Node] = None
self.tail: Optional[Node] = None

def push_head(self, value: str) -> Node:
new_node = Node(value)

if self.head is None:
self.head = new_node
self.tail = new_node
else:
new_node.next = self.head
self.head.previous = new_node
self.head = new_node

return new_node

def pop_tail(self) -> str:
if self.tail is None:
raise IndexError("Cannot pop from an empty list")

removed_node = self.tail
self.remove(removed_node)

return removed_node.value

def remove(self, node: Node) -> None:
if node.previous is None:
self.head = node.next
else:
node.previous.next = node.next

if node.next is None:
self.tail = node.previous
else:
node.next.previous = node.previous

node.next = None
node.previous = None
Loading