-
Notifications
You must be signed in to change notification settings - Fork 391
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
Spruce-C16 | Zandra | LinkedList #10
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 |
---|---|---|
|
@@ -2,99 +2,215 @@ | |
# Defines a node in the singly linked list | ||
class Node: | ||
|
||
def __init__(self, value, next_node = None): | ||
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 | ||
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 == 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): | ||
Comment on lines
+29
to
32
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 | ||
# This method adds a new node with the given value to the head of the list | ||
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
+40
to
43
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 | ||
# This method returns True or False if the list contains the given value. | ||
if self.head == None: | ||
return False | ||
|
||
current = self.head | ||
|
||
while current != None: | ||
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): | ||
Comment on lines
+58
to
61
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 | ||
# This method returns the size of the list. | ||
current = self.head | ||
count = 0 | ||
|
||
while current != None: | ||
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(1)? | ||
|
||
def get_at_index(self, index): | ||
Comment on lines
+75
to
78
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 | ||
# This method returns the value of the node at the given index. It returns None if the list does not have that many elements. | ||
if index < 0: | ||
return None | ||
|
||
current_index = 0 | ||
current = self.head | ||
|
||
while current is not None and current_index < index: | ||
current = current.next | ||
current_index += 1 | ||
|
||
if current is None: | ||
return None | ||
|
||
return current.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
+97
to
100
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. 👍 , just a note for this method and |
||
pass | ||
# This method returns the value of the last node in the list. | ||
if self.head == None: | ||
return None | ||
|
||
current = self.head | ||
|
||
while current.next != None: | ||
current = current.next | ||
|
||
return current.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(n)? | ||
|
||
def add_last(self, value): | ||
Comment on lines
+113
to
116
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 here is O(1) |
||
pass | ||
# This method adds a new node to the rear of the list. | ||
if self.head == None: | ||
new_node = Node(value) | ||
self.head = new_node | ||
return | ||
|
||
current = self.head | ||
|
||
while current.next != None: | ||
current = current.next | ||
|
||
# new_node = Node(value) | ||
# current.next = new_node | ||
current.next = Node(value) | ||
|
||
# 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 | ||
# This method finds the largest value in the list, assuming you can use >, or < to compare each element in the list. | ||
if self.head == None: | ||
return None | ||
|
||
current = self.head | ||
max_value = -100 | ||
|
||
while current != None: | ||
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: O(n)? | ||
# Space Complexity: O(1)? | ||
def delete(self, value): | ||
Comment on lines
+152
to
154
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 the first node is the one we want to delete, that's a | ||
# special case. | ||
if self.head and self.head.value == value: | ||
self.head = self.head.next | ||
return | ||
|
||
# Now iterate through the rest of the list and check | ||
# for the target value at the node _after_ the current one. | ||
current = self.head | ||
while current: | ||
if current.next and current.next.value == value: | ||
current.next = current.next.next | ||
return | ||
|
||
current = current.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): | ||
Comment on lines
+173
to
176
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 | ||
|
||
while current: | ||
helper_list.append(str(current.value)) | ||
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: ? | ||
# Time Complexity: O(n) ? | ||
# Space Complexity: O(1)? | ||
|
||
def reverse(self): | ||
Comment on lines
+188
to
191
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 think about better variable names than |
||
pass | ||
|
||
# If the list is empty or has only one element, then there is nothing to do | ||
if not self.head or not self.head.next: | ||
return | ||
|
||
# The list is of length AT LEAST 2. Time to reverse | ||
# the pointers | ||
p1 = self.head | ||
p2 = self.head.next | ||
p1.next = None # this is now the end of the new list | ||
while p2: | ||
temp = p2.next | ||
p2.next = p1 # p2 now points at the previous node | ||
p1 = p2 # advance p1 to the next node (in the old ordering) | ||
p2 = temp | ||
|
||
self.head = p1 | ||
|
||
## Advanced/ Exercises | ||
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
|
||
def find_middle_value(self): | ||
pass | ||
|
||
|
@@ -125,4 +241,4 @@ def create_cycle(self): | |
while current.next != None: | ||
current = current.next | ||
|
||
current.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.
👍