File tree 1 file changed +10
-7
lines changed
data_structures/linked_list
1 file changed +10
-7
lines changed Original file line number Diff line number Diff line change 6
6
- Last node: points to null
7
7
"""
8
8
9
- from typing import Any
9
+ from typing import Generic , Optional , TypeVar
10
10
11
+ T = TypeVar ("T" )
11
12
12
- class Node :
13
- def __init__ (self , item : Any , next : Any ) -> None :
13
+
14
+ class Node (Generic [T ]):
15
+ def __init__ (self , item : T , next : Optional ["Node[T]" ]) -> None :
14
16
self .item = item
15
17
self .next = next
16
18
17
19
18
- class LinkedList :
20
+ class LinkedList ( Generic [ T ]) :
19
21
def __init__ (self ) -> None :
20
- self .head = None
22
+ self .head : Optional [ Node [ T ]] = None
21
23
self .size = 0
22
24
23
- def add (self , item : Any ) -> None :
25
+ def add (self , item : T ) -> None :
24
26
self .head = Node (item , self .head )
25
27
self .size += 1
26
28
27
- def remove (self ) -> Any :
29
+ def remove (self ) -> Optional [ T ] :
28
30
if self .is_empty ():
29
31
return None
30
32
else :
33
+ assert self .head is not None
31
34
item = self .head .item
32
35
self .head = self .head .next
33
36
self .size -= 1
You can’t perform that action at this time.
0 commit comments