Skip to content

Commit ce3cda4

Browse files
committed
refactor: add type hints to Node and LinkedList methods
1 parent b8086a3 commit ce3cda4

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎Sprint-2/implement_linked_list/linked_list.py‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
from typing import Any, Optional
12
# A "Node" is a single building block of our list.
23
# Each node holds a value and two pointers: 'next' (forward) and 'previous' (backward).
34
class Node:
45
__slots__ = ("value", "next", "previous")
5-
def __init__(self, value):
6-
self.value = value
7-
self.next = None
8-
self.previous = None
6+
def __init__(self, value: Any)-> None:
7+
self.value: Any = value
8+
self.next: Optional[Node] = None
9+
self.previous: Optional[Node] = None
910

1011

1112
# A Doubly Linked List keeps track of both ends (head and tail).
1213
class LinkedList:
13-
def __init__(self):
14-
self.head = None
15-
self.tail = None
14+
def __init__(self) -> None:
15+
self.head: Optional[Node] = None
16+
self.tail: Optional[Node] = None
1617

17-
def push_head(self, value):
18+
def push_head(self, value: Any):
1819
"""Add a new item to the front of the list in O(1) time."""
1920
new_node = Node(value)
2021

@@ -32,7 +33,7 @@ def push_head(self, value):
3233
# We return the node itself so it can be used as a "handle" later
3334
return new_node
3435

35-
def pop_tail(self):
36+
def pop_tail(self) -> Any:
3637
"""Remove and return the item from the end of the list in O(1) time."""
3738
# If the list is empty, there is nothing to remove
3839
if self.tail is None:
@@ -46,7 +47,7 @@ def pop_tail(self):
4647

4748
return removed_value
4849

49-
def remove(self, node):
50+
def remove(self, node: Node) -> None :
5051
"""Remove a specific node from the list using its reference in O(1) time."""
5152

5253
# Scenario 1: It's the only item in the list

0 commit comments

Comments
 (0)