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
1313
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
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
1717
1818class LinkedList :
19- def __init__ (self ):
20- # reference to the head of the list
21- self .head = None
19+ def __init__ (self ):
20+ # reference to the head of the list
21+ self .head = None
2222
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
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
2929
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
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
4444
45- def reverse_list (self ):
46- # TO BE COMPLETED
47- pass
45+ def reverse_list (self ):
46+ if self .head :
47+ previous_node = None
48+ current_node = self .head
49+ next_node = self .head .next_node
50+ while next_node is not None :
51+ #reverse current_node
52+ current_node .next_node = previous_node
53+ #increment current and next_node
54+ previous_node = current_node
55+ current_node = next_node
56+ next_node = current_node .next_node
57+ current_node .next_node = previous_node
58+ #set current_node to head
59+ self .head = current_node
0 commit comments