Skip to content

Commit 36b9252

Browse files
committed
change reverse sll to require recursion
1 parent 37d6a73 commit 36b9252

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

reverse/reverse.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
class 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

1819
class 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

reverse/test_reverse.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def test_contains(self):
2020
self.assertFalse(self.list.contains(1000))
2121

2222
def test_empty_reverse(self):
23-
self.list.reverse_list()
23+
self.list.reverse_list(self.list.head, None)
2424
self.assertEqual(self.list.head, None)
2525

2626
def test_single_reverse(self):
2727
self.list.add_to_head(1)
28-
self.list.reverse_list()
28+
self.list.reverse_list(self.list.head, None)
2929
self.assertEqual(self.list.head.value, 1)
3030

3131
def test_longer_reverse(self):
@@ -35,7 +35,7 @@ def test_longer_reverse(self):
3535
self.list.add_to_head(4)
3636
self.list.add_to_head(5)
3737
self.assertEqual(self.list.head.value, 5)
38-
self.list.reverse_list()
38+
self.list.reverse_list(self.list.head, None)
3939
self.assertEqual(self.list.head.value, 1)
4040
self.assertEqual(self.list.head.get_next().value, 2)
4141
self.assertEqual(self.list.head.get_next().get_next().value, 3)

0 commit comments

Comments
 (0)