-
Notifications
You must be signed in to change notification settings - Fork 66
Rock - Delia #46
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
base: master
Are you sure you want to change the base?
Rock - Delia #46
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 |
|---|---|---|
|
|
@@ -13,105 +13,181 @@ 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): | ||
| pass | ||
|
|
||
| if self.head: | ||
| return self.head.value | ||
| else: | ||
| return None | ||
|
|
||
| # 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): | ||
|
Comment on lines
+26
to
28
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 | ||
| 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): | ||
|
Comment on lines
+35
to
37
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 not self.head: | ||
| return False | ||
| else: | ||
| curr_node = self.head | ||
| while curr_node != None: | ||
| if curr_node.value == value: | ||
| return True | ||
| curr_node = curr_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): | ||
|
Comment on lines
+49
to
51
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 | ||
| curr_node = self.head | ||
| len = 0 | ||
| while curr_node != None: | ||
| len += 1 | ||
| curr_node = curr_node.next | ||
| return len | ||
|
|
||
| # 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): | ||
|
Comment on lines
+62
to
64
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 index >= self.length(): | ||
| print("The index is out of range") | ||
| return None | ||
| curr_index = 0 | ||
| curr_node = self.head | ||
| while index > curr_index: | ||
| curr_node = curr_node.next | ||
| curr_index += 1 | ||
| if curr_index == index: | ||
| return curr_node.value | ||
|
|
||
| # 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): | ||
|
Comment on lines
+78
to
80
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 not self.head: | ||
| return None | ||
| curr_node = self.head | ||
| while curr_node.next != None: | ||
| curr_node = curr_node.next | ||
| return curr_node.value | ||
|
|
||
| # 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): | ||
|
Comment on lines
+89
to
91
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 | ||
| new_node = Node(value) | ||
| if not self.head: | ||
| self.head = new_node | ||
| return | ||
| curr_node = self.head | ||
| while curr_node.next != None: | ||
| curr_node = curr_node.next | ||
| curr_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): | ||
|
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 not self.head: | ||
| return None | ||
| curr_node = self.head | ||
| max_value = self.head.value | ||
| while curr_node != None: | ||
| if curr_node.value > max_value: | ||
| max_value = curr_node.value | ||
| curr_node = curr_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): | ||
|
Comment on lines
+115
to
117
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 not self.head: | ||
| return None | ||
| curr_node = self.head | ||
| if value == curr_node.value: | ||
| self.head = curr_node.next | ||
| return | ||
| while curr_node != None: | ||
| prev_node = curr_node | ||
| curr_node = curr_node.next | ||
| if value == curr_node.value: | ||
| prev_node.next = curr_node.next | ||
| return | ||
|
|
||
| # 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): | ||
|
Comment on lines
+132
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. 👍 |
||
| helper_list = [] | ||
| current = self.head | ||
| curr_node = self.head | ||
|
|
||
| while current: | ||
| helper_list.append(str(current.value)) | ||
| current = current.next | ||
| while curr_node: | ||
| helper_list.append(str(curr_node.value)) | ||
| curr_node = curr_node.next | ||
|
|
||
| print(", ".join(helper_list)) | ||
| return 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: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def reverse(self): | ||
|
Comment on lines
+147
to
149
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. 👍 However the space complexity is O(1) |
||
| pass | ||
| prev_node = None | ||
| curr_node = self.head | ||
| while curr_node != None: | ||
| next_node = curr_node.next | ||
| curr_node.next = prev_node | ||
| prev_node = curr_node | ||
| curr_node = next_node | ||
| self.head = prev_node | ||
|
|
||
| ## Advanced/ Exercises | ||
| # returns the value at the middle element in the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def find_middle_value(self): | ||
|
Comment on lines
+161
to
163
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. 👍 Good reuse of existing methods |
||
| pass | ||
| len_list = self.length() | ||
| index = len_list // 2 | ||
| return self.get_at_index(index) | ||
|
|
||
| # find the nth node from the end and return its value | ||
| # assume indexing starts at 0 while counting to n | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def find_nth_from_end(self, n): | ||
|
Comment on lines
+170
to
172
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 | ||
| len_list = self.length() | ||
| index = len_list - 1 - n | ||
| return self.get_at_index(index) | ||
|
|
||
| # 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: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def has_cycle(self): | ||
|
Comment on lines
+180
to
182
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. 👍 , Nice! |
||
| pass | ||
| slow_p = self.head | ||
| fast_p = self.head | ||
| while fast_p and fast_p.next != None: | ||
| slow_p = slow_p.next | ||
| fast_p = fast_p.next.next | ||
| if slow_p == fast_p: | ||
| return True | ||
| return False | ||
|
|
||
| # Helper method for tests | ||
| # Creates a cycle in the linked list for testing purposes | ||
|
|
@@ -121,8 +197,9 @@ def create_cycle(self): | |
| return | ||
|
|
||
| # navigate to last node | ||
| current = self.head | ||
| while current.next != None: | ||
| current = current.next | ||
| curr_node = self.head | ||
| while curr_node.next != None: | ||
| curr_node = curr_node.next | ||
|
|
||
| curr_node.next = self.head # make the last node link to first node | ||
|
|
||
| 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.
👍