-
Notifications
You must be signed in to change notification settings - Fork 86
Cedar - Maria O. #78
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?
Cedar - Maria O. #78
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 calendar import c | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| class Node: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| def __init__(self, value, next_node = None): | ||||||||||||||||||||
|
|
@@ -9,71 +12,150 @@ 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 not self.head: | ||||||||||||||||||||
| 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): | ||||||||||||||||||||
|
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): | ||||||||||||||||||||
|
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 | ||||||||||||||||||||
| # get head and iterate through nodes until right value | ||||||||||||||||||||
| current_node = self.head | ||||||||||||||||||||
| if not current_node: | ||||||||||||||||||||
| return False | ||||||||||||||||||||
|
|
||||||||||||||||||||
| while current_node.value != value: | ||||||||||||||||||||
| if current_node.next: | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return False | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return True | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # 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): | ||||||||||||||||||||
| pass | ||||||||||||||||||||
| # start counter, go through list, if next increase counter | ||||||||||||||||||||
| current_node = self.head | ||||||||||||||||||||
| length = 0 | ||||||||||||||||||||
| while current_node: | ||||||||||||||||||||
| length += 1 | ||||||||||||||||||||
| if current_node.next: | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return length | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return length | ||||||||||||||||||||
|
Comment on lines
+64
to
+69
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. 🤓 Style suggestion to condense your code. Since your while condition is essentially to check if If it's
Suggested change
|
||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| # 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): | ||||||||||||||||||||
|
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 | ||||||||||||||||||||
| # use counter, when counter matches index, return | ||||||||||||||||||||
| current_node = self.head | ||||||||||||||||||||
| counter = 0 | ||||||||||||||||||||
| if not current_node: | ||||||||||||||||||||
| return None | ||||||||||||||||||||
| elif counter == index: | ||||||||||||||||||||
| return current_node.value | ||||||||||||||||||||
| while counter != index: | ||||||||||||||||||||
| if current_node.next: | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return None | ||||||||||||||||||||
| counter += 1 | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return current_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): | ||||||||||||||||||||
|
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 | ||||||||||||||||||||
| if not current_node: | ||||||||||||||||||||
| return None | ||||||||||||||||||||
| while current_node.next: | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| return current_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): | ||||||||||||||||||||
|
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 | ||||||||||||||||||||
| new_node = Node(value) | ||||||||||||||||||||
| if not current_node: | ||||||||||||||||||||
| self.head = new_node | ||||||||||||||||||||
| return | ||||||||||||||||||||
| # self.add_first(value) | ||||||||||||||||||||
| # return | ||||||||||||||||||||
| while current_node.next: | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| current_node.next = new_node | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # method to return the max value in the linked list | ||||||||||||||||||||
| # returns the data value and not the node | ||||||||||||||||||||
| # Time Complexity: O(n) | ||||||||||||||||||||
| # Space Complexity: O(1) | ||||||||||||||||||||
| 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 | ||||||||||||||||||||
| current_node = self.head | ||||||||||||||||||||
| if not current_node: | ||||||||||||||||||||
| return None | ||||||||||||||||||||
| current_max = -900000000 | ||||||||||||||||||||
| while current_node != None: | ||||||||||||||||||||
| if current_max < current_node.value: | ||||||||||||||||||||
| current_max = current_node.value | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
| return current_max | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| # 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): | ||||||||||||||||||||
| pass | ||||||||||||||||||||
| current_node = self.head | ||||||||||||||||||||
| if not current_node: | ||||||||||||||||||||
| return None | ||||||||||||||||||||
| while current_node: | ||||||||||||||||||||
| if current_node.value == value: | ||||||||||||||||||||
| self.head = current_node.next | ||||||||||||||||||||
| break | ||||||||||||||||||||
|
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. 🤓I would suggest using return over break statements
Suggested change
|
||||||||||||||||||||
| elif current_node.next.value == value: | ||||||||||||||||||||
| current_node.next = current_node.next.next | ||||||||||||||||||||
| break | ||||||||||||||||||||
| else: | ||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # 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): | ||||||||||||||||||||
|
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 | ||||||||||||||||||||
|
|
@@ -86,11 +168,22 @@ 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(1) | ||||||||||||||||||||
| def reverse(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_node = self.head | ||||||||||||||||||||
| previous_node = None | ||||||||||||||||||||
| next_node = current_node.next | ||||||||||||||||||||
|
|
||||||||||||||||||||
| while current_node: | ||||||||||||||||||||
| current_node.next = previous_node | ||||||||||||||||||||
| previous_node = current_node | ||||||||||||||||||||
| current_node = next_node | ||||||||||||||||||||
| if next_node: | ||||||||||||||||||||
| next_node = next_node.next | ||||||||||||||||||||
|
|
||||||||||||||||||||
| self.head = previous_node | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ## Advanced/ Exercises | ||||||||||||||||||||
| # returns the value at the middle element in the singly linked list | ||||||||||||||||||||
| # Time Complexity: ? | ||||||||||||||||||||
|
|
||||||||||||||||||||
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.
✨