-
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
Ro Linked List Python #58
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,79 +1,118 @@ | ||
|
||
# Defines a node in the singly linked list | ||
|
||
class Node: | ||
|
||
def __init__(self, value, next_node = None): | ||
self.value = value | ||
self.next = next_node | ||
|
||
# Defines the singly linked list | ||
class LinkedList: | ||
def __init__(self): | ||
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: ? | ||
self.head = None | ||
def get_first(self): | ||
pass | ||
|
||
if self.head: | ||
return self.head.value | ||
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): | ||
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(1) | ||
# Space Complexity: O(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(n) here since you have to loop through all of the nodes in the worst case |
||
def search(self, value): | ||
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(n) | ||
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. 🪐 Space complexity is O(1) here since you aren't creating any additional data structures in your function body that are proportional to the size of the input |
||
def length(self): | ||
pass | ||
count = 0 | ||
current = self.head | ||
while current: | ||
count += 1 | ||
current = current.next | ||
|
||
return 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(n) | ||
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. 🪐 Same as |
||
pass | ||
current_index = 0 | ||
current = self.head | ||
while current_index < index and current: | ||
current_index += 1 | ||
current = current.next | ||
|
||
if current_index == index and current: | ||
return current.value | ||
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(n) | ||
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 as previous two functions on space complexity - it would be O(1) |
||
def get_last(self): | ||
pass | ||
if not self.head: | ||
return None | ||
|
||
current = self.head | ||
while current.next: | ||
current = current.next | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
return current.value | ||
|
||
# Time Complexity: O(n); more than 1 approach; same complexity | ||
# Space Complexity: O(n); more than 1 approach; same complexity | ||
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. 🪐 Again on space complexity - O(1) |
||
pass | ||
if not self.head: | ||
self.add_first(value) | ||
else: | ||
new_node = Node(value) | ||
current = self.head | ||
while current.next: | ||
current = current.next | ||
current.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. ⏱🪐 Time and space complexity? |
||
pass | ||
if self.head == None: | ||
return None | ||
|
||
current = self.head | ||
max = self.head.value | ||
while current: | ||
if current.value > max: | ||
max = current.value | ||
current = current.next | ||
|
||
return max | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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 - O(1) space complexity |
||
def delete(self, value): | ||
pass | ||
if not self.head: | ||
return | ||
|
||
if value == 0: | ||
self.head = self.head.next | ||
|
||
current = self.head | ||
current_index = 0 | ||
while current.next and current_index < value - 1: | ||
current = current.next | ||
current_index += 1 | ||
|
||
if current.next: | ||
current.next = current.next.next | ||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(1) after element is found; O(n) prior | ||
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(n) because this function iterates through the entire list and adds them to an array to print out |
||
# Space Complexity: O(1) | ||
def visit(self): | ||
helper_list = [] | ||
current = self.head | ||
|
@@ -84,45 +123,53 @@ def visit(self): | |
|
||
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: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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. 🪐 Space complexity would be O(1) here again because you aren't creating any additional data structures whose size is proportional to the size of your linked list |
||
def reverse(self): | ||
pass | ||
|
||
## Advanced/ Exercises | ||
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
if not self.head: | ||
return | ||
|
||
current = self.head | ||
previous = None | ||
|
||
while current: | ||
next = current.next | ||
current.next = previous | ||
previous = current | ||
current = next | ||
|
||
self.head = previous | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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. 🪐Space complexity is O(1) again |
||
def find_middle_value(self): | ||
pass | ||
if not self.head: | ||
return None | ||
|
||
return self.get_at_index(int(self.length() / 2)) | ||
|
||
# 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(n) | ||
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 | ||
if not self.head: | ||
return None | ||
|
||
# navigate to last node | ||
current = self.head | ||
while current.next != None: | ||
trail = None | ||
spacing = 0 | ||
while current and spacing < n: | ||
current = current.next | ||
spacing += 1 | ||
|
||
if not current: | ||
return None | ||
|
||
trail = self.head | ||
while current.next: | ||
current = current.next | ||
trail = trail.next | ||
|
||
return trail.value | ||
|
||
current.next = self.head # make the last node link to first node | ||
# Time Complexity: O(log n) | ||
# Space Complexity: O(log n) |
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.
⏱🪐Time and space complexity?