-
Notifications
You must be signed in to change notification settings - Fork 87
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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): | ||||
|
@@ -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): | ||||
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): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, a doubly linked list maintains |
||||
|
||||
|
||||
# method to return the max value in the linked list | ||||
# returns the data value and not the node | ||||
def find_max(self): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||
temp = current.next | ||||
break | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not necessary to return anything for this function
Suggested change
|
||||
|
||||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||
def visit(self): | ||||
helper_list = [] | ||||
current = self.head | ||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
|
||||
## 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨