1
1
# Program to print the elements of a linked list in reverse
2
2
3
+ from typing import List
3
4
4
5
class Node :
5
6
def __init__ (self , data = None ):
@@ -17,9 +18,25 @@ def __repr__(self):
17
18
return string_rep
18
19
19
20
20
- def make_linked_list (elements_list ):
21
+ def make_linked_list (elements_list : List ):
21
22
"""Creates a Linked List from the elements of the given sequence
22
23
(list/tuple) and returns the head of the Linked List."""
24
+ """
25
+ >>> make_linked_list()
26
+ Traceback (most recent call last):
27
+ ...
28
+ Exception: The Elements List is empty
29
+ >>> make_linked_list([])
30
+ Traceback (most recent call last):
31
+ ...
32
+ Exception: The Elements List is empty
33
+ >>> make_linked_list([7])
34
+ '<7> ---> <END>'
35
+ >>> make_linked_list(['abc'])
36
+ '<abc> ---> <END>'
37
+ >>> make_linked_list([7, 25])
38
+ '<7> ---> <25> ---> <END>'
39
+ """
23
40
24
41
# if elements_list is empty
25
42
if not elements_list :
@@ -37,11 +54,17 @@ def make_linked_list(elements_list):
37
54
return head
38
55
39
56
40
- def print_reverse (head_node ):
57
+ def print_reverse (head_node : Node ):
41
58
"""Prints the elements of the given Linked List in reverse order"""
59
+ """
60
+ >>> print_reverse()
61
+ None
62
+ >>> print_reverse([])
63
+ None
64
+ """
42
65
43
66
# If reached end of the List
44
- if head_node is None :
67
+ if head_node is None or not isinstance ( head_node , Node ) :
45
68
return None
46
69
else :
47
70
# Recurse
0 commit comments