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).
34class 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).
1213class 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