-
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
all tests passing #66
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 unittest.mock import NonCallableMagicMock | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
class Node: | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
def __init__(self, value, next_node = None): | ||||||||||||||||||||||||
|
@@ -9,67 +12,127 @@ 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 | ||||||||||||||||||||||||
self.tail = None | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# returns the value in the first node | ||||||||||||||||||||||||
# returns None if the list is empty | ||||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||
Comment on lines
30
to
31
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
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, self.head) | ||||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||||
self.tail = new_node | ||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||
Comment on lines
40
to
41
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
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 != None: | ||||||||||||||||||||||||
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! one way we can shorten this is to:
Suggested change
|
||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||
Comment on lines
51
to
52
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
def length(self): | ||||||||||||||||||||||||
pass | ||||||||||||||||||||||||
temp = self.head | ||||||||||||||||||||||||
count = 0 | ||||||||||||||||||||||||
while temp: | ||||||||||||||||||||||||
count += 1 | ||||||||||||||||||||||||
temp = temp.next | ||||||||||||||||||||||||
Comment on lines
+54
to
+58
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 works fine! just curious why you named it |
||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||
Comment on lines
64
to
65
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
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 = self.head | ||||||||||||||||||||||||
count = 0 | ||||||||||||||||||||||||
while current: | ||||||||||||||||||||||||
if count == index: | ||||||||||||||||||||||||
return current.value | ||||||||||||||||||||||||
count += 1 | ||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||
Comment on lines
78
to
79
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
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 | ||||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||||
return None | ||||||||||||||||||||||||
return self.tail.value | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
# method that inserts a given value as a new last node in the linked list | ||||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||||
Comment on lines
86
to
87
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
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 = self.head | ||||||||||||||||||||||||
new_node = Node(value) | ||||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||||
self.tail = new_node | ||||||||||||||||||||||||
self.head = new_node | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
while current.next != None: | ||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||
current.next = new_node | ||||||||||||||||||||||||
self.tail = current.next | ||||||||||||||||||||||||
Comment on lines
+95
to
+98
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. do we need this while loop? we already have a pointer to the last node because of
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
# 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 | ||||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||||
return None | ||||||||||||||||||||||||
max = 0 | ||||||||||||||||||||||||
while current != None: | ||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||
Comment on lines
114
to
115
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
def delete(self, value): | ||||||||||||||||||||||||
pass | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if self.head == None: | ||||||||||||||||||||||||
return None | ||||||||||||||||||||||||
if self.head.value == value: | ||||||||||||||||||||||||
self.head = self.head.next | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||||
while current.next != None: | ||||||||||||||||||||||||
if current.value == value: | ||||||||||||||||||||||||
current.next = current.next.next | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
prev = current | ||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||
if current.value == value: | ||||||||||||||||||||||||
prev.next = None | ||||||||||||||||||||||||
self.tail = prev | ||||||||||||||||||||||||
Comment on lines
+126
to
+133
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 want to stop a node earlier in order to drop the deleted node in one line:
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
# method to print all the values in the linked list | ||||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||||
|
@@ -89,7 +152,15 @@ def visit(self): | |||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||||
Comment on lines
152
to
153
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. What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input? |
||||||||||||||||||||||||
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 | ||||||||||||||||||||||||
previous = None | ||||||||||||||||||||||||
current = self.head | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
while current != None: | ||||||||||||||||||||||||
next = current.next | ||||||||||||||||||||||||
current.next = previous | ||||||||||||||||||||||||
previous = current | ||||||||||||||||||||||||
current = next | ||||||||||||||||||||||||
self.head = previous | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
## Advanced/ Exercises | ||||||||||||||||||||||||
# returns the value at the middle element in the singly linked list | ||||||||||||||||||||||||
|
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.
What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?