Skip to content

Commit 0b4376c

Browse files
committed
Remove constraint that reverse needs to be implemented recursively
1 parent 045b6d7 commit 0b4376c

File tree

5 files changed

+7
-180
lines changed

5 files changed

+7
-180
lines changed

reverse/reverse.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class Node:
22
def __init__(self, value=None, next_node=None):
3-
# the value at this linked list node
43
self.value = value
5-
# reference to the next node in the list
64
self.next_node = next_node
75

86
def get_value(self):
@@ -12,17 +10,15 @@ def get_next(self):
1210
return self.next_node
1311

1412
def set_next(self, new_next):
15-
# set this node's next_node reference to the passed in node
1613
self.next_node = new_next
1714

18-
1915
class LinkedList:
2016
def __init__(self):
21-
# reference to the head of the list
2217
self.head = None
2318

2419
def add_to_head(self, value):
2520
node = Node(value)
21+
2622
if self.head is not None:
2723
node.set_next(self.head)
2824

@@ -31,20 +27,16 @@ def add_to_head(self, value):
3127
def contains(self, value):
3228
if not self.head:
3329
return False
34-
# get a reference to the node we're currently at; update this as we
35-
# traverse the list
30+
3631
current = self.head
37-
# check to see if we're at a valid node
32+
3833
while current:
39-
# return True if the current value we're looking at matches our
40-
# target value
4134
if current.get_value() == value:
4235
return True
43-
# update our current node to the current node's next node
36+
4437
current = current.get_next()
45-
# if we've gotten here, then the target node isn't in our list
38+
4639
return False
4740

4841
def reverse_list(self, node, prev):
49-
# You must use recursion for this solution
5042
pass

reverse/test_reverse.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,5 @@ def test_longer_reverse(self):
4040
self.assertEqual(self.list.head.get_next().value, 2)
4141
self.assertEqual(self.list.head.get_next().get_next().value, 3)
4242

43-
44-
45-
4643
if __name__ == '__main__':
4744
unittest.main()

ring_buffer/doubly_linked_list.py

Lines changed: 0 additions & 138 deletions
This file was deleted.

ring_buffer/ring_buffer.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,9 @@
1-
from doubly_linked_list import DoublyLinkedList
2-
3-
41
class RingBuffer:
5-
def __init__(self, capacity):
6-
self.capacity = capacity
7-
self.current = None
8-
self.storage = DoublyLinkedList()
9-
10-
def append(self, item):
11-
pass
12-
13-
def get(self):
14-
# Note: This is the only [] allowed
15-
list_buffer_contents = []
16-
17-
# TODO: Your code here
18-
19-
return list_buffer_contents
20-
21-
# ----------------Stretch Goal-------------------
22-
23-
24-
class ArrayRingBuffer:
252
def __init__(self, capacity):
263
pass
274

285
def append(self, item):
296
pass
307

318
def get(self):
32-
pass
9+
pass

ring_buffer/test_ring_buffer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import unittest
2-
from ring_buffer import RingBuffer, ArrayRingBuffer
3-
2+
from ring_buffer import RingBuffer
43

54
class RingBufferTests(unittest.TestCase):
65
def setUp(self):

0 commit comments

Comments
 (0)