-
-
Save codecademydev/71cbe4ab58e0cc32746b3ad9364c7c67 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = "" | |
current_node = self.head_node | |
while current_node: | |
string += str(current_node.value) + "\n" | |
current_node = current_node.next_node | |
return string | |
''' 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 = "" | |
current_node = self.get_head_node() | |
while current_node: | |
if current_node.get_value() != None: | |
string_list += str(current_node.get_value()) + "\n" | |
current_node = current_node.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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment