11class Node :
2- def __init__ (self , value = None , next_node = None ):
3- # the value at this linked list node
4- self .value = value
5- # reference to the next node in the list
6- self .next_node = next_node
2+ def __init__ (self , value = None , next_node = None ):
3+ # the value at this linked list node
4+ self .value = value
5+ # reference to the next node in the list
6+ self .next_node = next_node
77
8- def get_value (self ):
9- return self .value
8+ def get_value (self ):
9+ return self .value
1010
11- def get_next (self ):
12- return self .next_node
11+ def get_next (self ):
12+ return self .next_node
13+
14+ def set_next (self , new_next ):
15+ # set this node's next_node reference to the passed in node
16+ self .next_node = new_next
1317
14- def set_next (self , new_next ):
15- # set this node's next_node reference to the passed in node
16- self .next_node = new_next
1718
1819class LinkedList :
19- def __init__ (self ):
20- # reference to the head of the list
21- self .head = None
22-
23- def add_to_head (self , value ):
24- node = Node (value )
25- if self .head is not None :
26- node .set_next (self .head )
27-
28- self .head = node
29-
30- def contains (self , value ):
31- if not self .head :
32- return False
33- # get a reference to the node we're currently at; update this as we traverse the list
34- current = self .head
35- # check to see if we're at a valid node
36- while current :
37- # return True if the current value we're looking at matches our target value
38- if current .get_value () == value :
39- return True
40- # update our current node to the current node's next node
41- current = current .get_next ()
42- # if we've gotten here, then the target node isn't in our list
43- return False
44-
45- def reverse_list (self ):
46- # TO BE COMPLETED
47- pass
20+ def __init__ (self ):
21+ # reference to the head of the list
22+ self .head = None
23+
24+ def add_to_head (self , value ):
25+ node = Node (value )
26+ if self .head is not None :
27+ node .set_next (self .head )
28+
29+ self .head = node
30+
31+ def contains (self , value ):
32+ if not self .head :
33+ return False
34+ # get a reference to the node we're currently at; update this as we
35+ # traverse the list
36+ current = self .head
37+ # check to see if we're at a valid node
38+ while current :
39+ # return True if the current value we're looking at matches our
40+ # target value
41+ if current .get_value () == value :
42+ return True
43+ # update our current node to the current node's next node
44+ current = current .get_next ()
45+ # if we've gotten here, then the target node isn't in our list
46+ return False
47+
48+ def reverse_list (self , node , prev ):
49+ # You must use recursion for this solution
50+ pass
0 commit comments