-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.py
42 lines (36 loc) · 1.16 KB
/
linkedlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# We'll be using our Node class
class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node
def get_value(self):
return self.value
def get_next_node(self):
return self.next_node
def set_next_node(self, next_node):
self.next_node = next_node
# Our LinkedList class
class LinkedList:
def __init__(self, value=None):
self.head_node = Node(value)
def get_head_node(self):
return self.head_node
# Add your insert_beginning and stringify_list methods below:
def insert_beginning(self, new_value):
new_node = Node(new_value)
new_node.set_next_node(self.head_node)
self.head_node = new_node
def stringify_list(self):
string_list = ''
currentnode = self.get_head_node()
while currentnode:
if currentnode.get_value() != None:
string_list += str(currentnode.get_value()) + "\n"
currentnode = currentnode.get_next_node()
return string_list
# Test your code by uncommenting the statements below - did your list print to the terminal?
ll = LinkedList(5)
ll.insert_beginning(70)
ll.insert_beginning(5675)
ll.insert_beginning(90)
print(ll.stringify_list())