Skip to content

Commit 6e4d93a

Browse files
committed
refactor: add type hints to Node and LinkedList classes
- Implement typing (Any, Optional) for parameters and return values - Add forward references for Node pointers
1 parent eb716e6 commit 6e4d93a

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

‎Sprint-2/implement_linked_list/linked_list.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1+
from typing import Any, Optional
2+
13
class Node:
24
"""
35
Represents a single person in line.
46
Stores a value and links to the people ahead and behind.
57
"""
68
__slots__ = ['value', 'next', 'previous']
79

8-
def __init__(self, value):
10+
def __init__(self, value: Any):
911
self.value = value
10-
self.next = None
11-
self.previous = None
12+
self.next: Optional['Node'] = None
13+
self.previous: Optional['Node'] = None
1214

1315

1416
class LinkedList:
1517
"""
1618
Manages the line by keeping track of the Head and the Tail.
1719
"""
1820
def __init__(self):
19-
self.head = None
20-
self.tail = None
21+
self.head: Optional[Node] = None
22+
self.tail: Optional[Node] = None
2123

22-
def push_head(self, value):
24+
def push_head(self, value: Any) -> Node:
2325
"""Adds a new node to the front of the list."""
2426
new_node = Node(value)
2527

@@ -34,7 +36,7 @@ def push_head(self, value):
3436

3537
return new_node
3638

37-
def pop_tail(self):
39+
def pop_tail(self) -> Optional[Any]:
3840
"""Removes the last node and returns its value."""
3941

4042
if self.tail is None:
@@ -52,7 +54,7 @@ def pop_tail(self):
5254

5355
return value_to_return
5456

55-
def remove(self, node):
57+
def remove(self, node: Optional[Node]) -> None:
5658
"""Removes a specific node from anywhere in the list."""
5759
if node is None:
5860
return

0 commit comments

Comments
 (0)