-
Notifications
You must be signed in to change notification settings - Fork 68
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
Rock - N.Lucuab #63
base: master
Are you sure you want to change the base?
Rock - N.Lucuab #63
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 |
---|---|---|
|
@@ -9,71 +9,161 @@ 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) - nothing new is being added, so memory is unaffected | ||
def get_first(self): | ||
pass | ||
# if the list is empty, return none | ||
if self.head == None: | ||
return None | ||
# if the list is not empty, return the value of the head | ||
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)? Only 1 node is being added, so 0(1) sounds right memory-wise | ||
def add_first(self, value): | ||
Comment on lines
+28
to
30
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, next_node=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(n) - traverses the whole list | ||
# Space Complexity: O(1) - nothing new is being added, so memory is unaffected | ||
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 | ||
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) - traverses the list | ||
# Space Complexity: O(1) - no new node is being added, so memory is unaffected | ||
def length(self): | ||
Comment on lines
+47
to
49
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 | ||
# set variable to 0, to initiate counter | ||
list_length = 0 | ||
while current != None: | ||
# add to counter | ||
list_length += 1 | ||
# set current to next to traverse | ||
current = current.next | ||
# return list_length when while loops breaks | ||
return list_length | ||
|
||
# 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) - traverses the list to find value | ||
# Space Complexity: O(1) - no new node is being added, so memory is unaffected | ||
def get_at_index(self, index): | ||
Comment on lines
+64
to
66
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 | ||
# return None if fewer nodes (can be equal!) than index value | ||
if index >= self.length(): | ||
return None | ||
count = 0 | ||
# check count versus index | ||
while current != None: | ||
if count == index: | ||
return current.value | ||
# set current to next | ||
current = current.next | ||
# increment count | ||
count += 1 | ||
|
||
# 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) - traverses list? | ||
# Space Complexity: O(1) - no new node is being added, so memory is unaffected | ||
def get_last(self): | ||
Comment on lines
+83
to
85
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: | ||
# if next is None, end of list, so return value | ||
if current.next == None: | ||
return current.value | ||
else: | ||
# set current to next, if next != None | ||
current = current.next | ||
# if list is empty return None | ||
return None | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(1) - only inserts 1 value | ||
# Space Complexity: O(n) - traverses the list in order to find the end, and then adds on value | ||
def add_last(self, value): | ||
Comment on lines
+98
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. 👍 The time complexity is O(n) because you traverse to the end of the list. The space complexity is O(1) because you only ever add one node. |
||
pass | ||
# can set new_node to head, in the case of an empty list. That value also becomes 'last' | ||
new_node = Node(value) | ||
current = self.head | ||
if current == None: | ||
self.head = new_node | ||
return | ||
# set current to next, while not currently at end of list | ||
while current.next != None: | ||
current = current.next | ||
# set current.next to the new_node | ||
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. 👍 |
||
pass | ||
# set up an empty variable | ||
max_value = 0 | ||
current = self.head | ||
# check if list empty, if so, we can return None | ||
if current == None: | ||
return None | ||
# when list is not empty, check current value against value in max_value | ||
# if max_value is less than current value, set max_value to the current value | ||
while current != None: | ||
if max_value < current.value: | ||
max_value = current.value | ||
# set current to next to traverse | ||
current = current.next | ||
# return max_value when loop breaks | ||
return max_value | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) - traverses list to find specified value | ||
# Space Complexity: O(1) - deletes only 1 value | ||
def delete(self, value): | ||
Comment on lines
+133
to
135
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 | ||
previous = None | ||
# check if list is empty | ||
if current == None: | ||
return | ||
|
||
# if not empty... | ||
while current != None: | ||
# check current value against passed in value | ||
if current.value == value: | ||
# if equal, check to see if end of list | ||
if current.next == None: | ||
previous.next = None | ||
return previous | ||
# set current value equal to the next node value | ||
current.value = current.next.value | ||
# set current next to the NEXT node | ||
current.next = current.next.next | ||
# continue loop | ||
continue | ||
# return current | ||
# set previous equal to the current node | ||
previous = current | ||
# increment current | ||
current = current.next | ||
return current | ||
|
||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) - traverses list | ||
# Space Complexity: O(n) - prints out every value | ||
Comment on lines
+165
to
+166
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. 👍 |
||
def visit(self): | ||
helper_list = [] | ||
current = self.head | ||
|
@@ -86,11 +176,21 @@ 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) - traverses/reverses entire list | ||
# Space Complexity: O(1) - no new nodes are being added | ||
def reverse(self): | ||
Comment on lines
+179
to
181
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 | ||
next_node = current.next | ||
|
||
# reversing node order | ||
while current != None: | ||
next_node = current.next | ||
current.next = previous | ||
previous = current | ||
current = next_node | ||
self.head = previous | ||
|
||
## 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.
👍