File tree 1 file changed +11
-7
lines changed
data_structures/linked_list
1 file changed +11
-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 TypeVar , Generic , Optional
10
10
11
11
12
- class Node :
13
- def __init__ (self , item : Any , next : Any ) -> None :
12
+ T = TypeVar ("T" )
13
+
14
+
15
+ class Node (Generic [T ]):
16
+ def __init__ (self , item : T , next : Optional ["Node[T]" ]) -> None :
14
17
self .item = item
15
18
self .next = next
16
19
17
20
18
- class LinkedList :
21
+ class LinkedList ( Generic [ T ]) :
19
22
def __init__ (self ) -> None :
20
- self .head = None
23
+ self .head : Optional [ Node [ T ]] = None
21
24
self .size = 0
22
25
23
- def add (self , item : Any ) -> None :
26
+ def add (self , item : T ) -> None :
24
27
self .head = Node (item , self .head )
25
28
self .size += 1
26
29
27
- def remove (self ) -> Any :
30
+ def remove (self ) -> Optional [ T ] :
28
31
if self .is_empty ():
29
32
return None
30
33
else :
34
+ assert self .head is not None
31
35
item = self .head .item
32
36
self .head = self .head .next
33
37
self .size -= 1
You can’t perform that action at this time.
0 commit comments