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

Maple: Sabrina Lauredan #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 87 additions & 72 deletions linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

# Defines a node in the singly linked list
from threading import currentThread


class Node:

def __init__(self, value, next_node = None):
Expand All @@ -9,71 +12,126 @@ def __init__(self, value, next_node = None):
# Defines the singly linked list
class LinkedList:
def __init__(self):
self.head = None # keep the head private. Not accessible outside this class
self.head = None # keep the head private. Not accessible outside this class

# 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

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
self.head = Node(value, self.head)


# 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 = self.head
while current:
if current.value == value:
return True
current = current.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
current = self.head
lenght_of_list = 0
while current != None:
lenght_of_list += 1
current = current.next
return lenght_of_list

# 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: 0(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
while current_node != None:
if current_index == index:
return current_node.value
current_index += 1
current_node = current_node.next
return current_node

# 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: 0(1)

Choose a reason for hiding this comment

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

⏱ Time complexity is O(1) for a doubly linked list. See comment below ⬇️ For a singly linked list, an O(1) time complexity for get_last is not possible. The best would be O(n) because you need to start at the first node in the list and traverse the entirety of the list until you find the end.

# Space Complexity: 0(1)
def get_last(self):
pass
if self.length() is 0:
return None
return self.tail.value
Comment on lines 78 to +81

Choose a reason for hiding this comment

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

This would work for a doubly linked list which maintains a tail pointer, but this is a singly linked list; we only have a head pointer to the first node in the list.

Instead, you need to traverse the entire list until you find the last node.


# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: 0(1)

Choose a reason for hiding this comment

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

For a singly linked list, the time complexity should be O(n). Same reasoning as for get_last above ⬆️

# Space Complexity: 0(1)
def add_last(self, value):
pass
if self.head is None:
self.head = Node(value)
else:
new_node = Node(value)
new_node.previous = self.tail
self.tail.next = new_node
self.tail = new_node
Comment on lines +91 to +93

Choose a reason for hiding this comment

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

Again, a doubly linked list maintains previous and tail pointers, but a singly linked list only has head and `next pointers. For a singly linked list, you need to start your traversal at the head and move through the list until you find the tail. Then redirect the current tail to what you want the new tail to be.



# 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.

pass
if self.head is None:
return None

current = self.head
max_value = current.value
while current:
if current.value > max_value:
max_value = current.value
current = current.next
return max_value

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: 0(n)
# Space Complexity: 0(1)
def delete(self, value):
pass
if self.head is None:
return
if value == 0:

Choose a reason for hiding this comment

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

🤔 value doesn't indicate the index of the node you are trying to delete. This function should delete the first node in the list with value value not index `value.

You would still want an edge case for if the node you want to delete is the head node, but you need to tweak your condition a bit here.

self.head = self.head.next
index = 0
current = self.head
prev = self.head
temp = self.head
while current is not None:
if index == value:
Comment on lines +118 to +123

Choose a reason for hiding this comment

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

Same thing here you are checking to see if the node's value attribute is equal to the passed in value, not to see whether value equals the index of the node.

temp = current.next
break

Choose a reason for hiding this comment

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

🤓 We generally like to avoid break statements where possible. How might you refactor your code to eliminate this break statement?

prev = current
current = current.next
index += 1
prev.next = temp
return prev

Choose a reason for hiding this comment

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

It's not necessary to return anything for this function

Suggested change
return prev


# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
Comment on lines +133 to +134

Choose a reason for hiding this comment

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

def visit(self):
helper_list = []
current = self.head
Expand All @@ -83,46 +141,3 @@ def visit(self):
current = current.next

print(", ".join(helper_list))

# 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: ?
def reverse(self):
pass
Comment on lines -89 to -92

Choose a reason for hiding this comment

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

⚠️ This is a required function


## Advanced/ Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def find_middle_value(self):
pass

# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: ?
# Space Complexity: ?
def find_nth_from_end(self, n):
pass

# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity: ?
# Space Complexity: ?
def has_cycle(self):
pass

# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle(self):
if self.head == None:
return

# navigate to last node
current = self.head
while current.next != None:
current = current.next

current.next = self.head # make the last node link to first node