Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monica Cruz - Maple C16 #12

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
187 changes: 153 additions & 34 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Defines a node in the singly linked list
class Node:

Expand All @@ -13,67 +12,157 @@ def __init__(self):

# returns the value in the first node
# returns None if the list is empty
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_first(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass

if self.head is None:
return None
else:
return self.head.value

# method to add a new node with the specific data value in the linked list
# insert the new node at the beginning of the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(1)
# Space Complexity: O(1)
def add_first(self, value):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass

new_node = Node(value)
new_node.next = self.head
self.head = new_node

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def search(self, value):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
current_node = self.head
while current_node is not None:
if current_node.value == value:
return True
current_node = current_node.next
return False


# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def length(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
node_count = 0
current_node = self.head
while current_node is not None:
node_count += 1
current_node = current_node.next
return node_count

# method that returns the value at a given index in the linked list
# index count starts at 0
# returns None if there are fewer nodes in the linked list than the index value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_at_index(self, index):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
current_node = self.head
current_index = 0

if current_node is None:
return None

while current_node is not None:
if current_index == index:
return current_node.value
else:
current_node = current_node.next
current_index+=1
return None

# method that returns the value of the last node in the linked list
# returns None if the linked list is empty
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_last(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ See comment on code style below ⬇️

pass
current_node = self.head

if current_node is None:
return None

while True:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if you know the loop will eventually exit, it's generally bad practice to use while True. With a while loop, we always want to make sure we are making progress inside the loop towards making the condition Falsy and that's impossible to do with while True

Suggested change
while True:
while current_node is not None:

if current_node.next is None:
return current_node.value
else:
current_node = current_node.next



# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def add_last(self, value):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
new_node = Node(value)
current_node = self.head

if current_node is None:
self.head = new_node
return

# What do I want while loop to do? I want while loop to find the last node

while current_node is not None:
if current_node.next is None:
break
current_node = current_node.next

# When last node is found, I want to append new_node to it.
current_node.next = new_node
Comment on lines +108 to +113

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another code style suggestion: we generally want to avoid break statements where possible

Suggested change
if current_node.next is None:
break
current_node = current_node.next
# When last node is found, I want to append new_node to it.
current_node.next = new_node
if current_node.next is None:
# When last node is found, I want to append new_node to it.
current_node.next = new_node
return
current_node = current_node.next
# When last node is found, I want to append new_node to it.
#current_node.next = new_node


# method to return the max value in the linked list
# returns the data value and not the node
def find_max(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ See code style suggestion below!

pass
current_node = self.head
if current_node is None:
return None

max_value = current_node.value

while current_node.next:
if current_node.value > max_value:
max_value = current_node.value
current_node = current_node.next

if current_node.value > max_value:
max_value = current_node.value
Comment on lines +124 to +130

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while current_node.next:
if current_node.value > max_value:
max_value = current_node.value
current_node = current_node.next
if current_node.value > max_value:
max_value = current_node.value
while current_node:
if current_node.value > max_value:
max_value = current_node.value
current_node = current_node.next


return max_value

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def delete(self, value):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass
current_node = self.head
previous_node = None

if current_node is None:
return None

# Case: value to be deleted is the first value in the linked list
if current_node.value == value:
self.head = current_node.next
current_node = None
return

# Case: value to be deleted is the last value in the linked list
# traverse linked list to find the last node
# compare speficied value to last node's value
# if the values match, re-assign the penultimate node reference to None
while current_node is not None and current_node.value != value:
previous_node = current_node
current_node = current_node.next

previous_node.next = current_node.next
current_node = None



# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def visit(self):
helper_list = []
current = self.head
Expand All @@ -86,11 +175,41 @@ def visit(self):

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def reverse(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✨ Very creative solution! If you wanted to refactor, think about how you might be able to do this with just one traversal of the list (one while loop). Hint: Pointers will play a big part!

pass


# Put nodes in a list
# Reverse the list
# Create a new linked list from the reversed list

current_node = self.head
nodes_list = []
nodes_list_reverse = []

while current_node is not None:
nodes_list.append(current_node)
current_node = current_node.next

i = len(nodes_list)-1

while i > 0:
nodes_list_reverse.append(nodes_list[i])
i -= 1
nodes_list_reverse.append(nodes_list[0])

for i in range(0, len(nodes_list_reverse)):
if i == 0:
current_node = nodes_list_reverse[i]
self.head = current_node
else:
current_node = nodes_list_reverse[i]

if i < len(nodes_list_reverse)-1:
current_node.next = nodes_list_reverse[i+1]

current_node.next = None

## Advanced/ Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
Expand Down