forked from AdaGold/linked-list
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Savannah #20
Open
qqdipps
wants to merge
18
commits into
Ada-C11:master
Choose a base branch
from
qqdipps:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Savannah #20
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bcc1aef
get first and add to front done
qqdipps 7ac042e
implements length method, tests green
qqdipps 0f7843e
get_last & add_last w/ green test
qqdipps 30477a7
get_at_index implemented, test passing
qqdipps 26428a4
min & max implemented, tests green
qqdipps 33b30cd
time/ sapce complexity analysis for implemented methods
qqdipps 9cec077
implemented delete method, tests green
qqdipps 707672a
added implementation for nth_from_end, tests green to go
qqdipps 744fb22
implemented reverse, wrote unit test for search and implemented method
qqdipps 25a932c
implemented modified visit method and unit tests
qqdipps 4effb5e
implemented find_middle_value with unit tests
qqdipps 5989312
cycle method implemented with unit tests
qqdipps 937c236
added tests and implementation for insert_ascending(value)
qqdipps 1663e18
updated space complex analysis to not include object link list
qqdipps 8d85edc
adds missing space/ time analysis for search method
qqdipps 43a669c
adds tail pointer and update methods and time complex
qqdipps f7ad98e
adds explicit return of nil when no return is expected as to not leak…
qqdipps 92f9dfb
edge and tail to reverse
qqdipps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,136 +14,284 @@ def initialize(value, next_node = nil) | |
class LinkedList | ||
def initialize | ||
@head = nil # keep the head private. Not accessible outside this class | ||
@tail = nil | ||
@length = 0 | ||
end | ||
|
||
# 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(value) | ||
raise NotImplementedError | ||
new_node = Node.new(value) | ||
if !@head | ||
@head = new_node | ||
@tail = new_node | ||
else | ||
new_node = Node.new(value) | ||
new_node.next = @head | ||
@head = new_node | ||
end | ||
@length += 1 | ||
return | ||
end | ||
|
||
# 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) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def search(value) | ||
raise NotImplementedError | ||
curr = @head | ||
while curr | ||
return true if curr.data == value | ||
curr = curr.next | ||
end | ||
return false | ||
end | ||
|
||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def find_max | ||
raise NotImplementedError | ||
return if !@head | ||
curr = @head | ||
max = curr.data | ||
while curr | ||
if max < curr.data | ||
max = curr.data | ||
end | ||
curr = curr.next | ||
end | ||
return max | ||
end | ||
|
||
# method to return the min value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def find_min | ||
raise NotImplementedError | ||
return if !@head | ||
curr = @head | ||
min = curr.data | ||
while curr | ||
if min > curr.data | ||
min = curr.data | ||
end | ||
curr = curr.next | ||
end | ||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(1) (was O(n) updated class to keep track on length) | ||
# Space Complexity: O(1) | ||
def length | ||
raise NotImplementedError | ||
# count = 0 | ||
# curr = @head | ||
# while curr && @head | ||
# curr = curr.next | ||
# count += 1 | ||
# end | ||
# return count | ||
return @length | ||
end | ||
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns nil if there are fewer nodes in the linked list than the index value | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def get_at_index(index) | ||
raise NotImplementedError | ||
target_node = node_at_index(index) | ||
return if !target_node | ||
return target_node.data | ||
end | ||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def node_at_index(index) | ||
length = length() | ||
return if length - 1 < index || index < 0 | ||
curr = @head | ||
count = 0 | ||
while index != count | ||
curr = curr.next | ||
count += 1 | ||
end | ||
return curr | ||
end | ||
|
||
# method to return a string of all the values in the linked list | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# 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. Since you're building a string of equal size to the list, you have a space complexity of O(n) |
||
def visit | ||
raise NotImplementedError | ||
node_values = "" | ||
curr = @head | ||
while curr | ||
node_values << curr.data.to_s | ||
curr = curr.next | ||
end | ||
return node_values | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def delete(value) | ||
raise NotImplementedError | ||
return if !@head | ||
curr = @head | ||
if curr.data == value | ||
delete_first_node() | ||
else | ||
while curr.next | ||
if curr.next.data == value | ||
remove_next_node(curr) | ||
else | ||
curr = curr.next | ||
end | ||
end | ||
end | ||
@length -= 1 | ||
return | ||
end | ||
|
||
def delete_first_node | ||
temp = @head | ||
@head = @head.next | ||
temp.next = nil | ||
end | ||
|
||
def remove_next_node(node) | ||
temp = node.next | ||
node.next = node.next.next | ||
temp.next = nil | ||
@tail = node if !node.next | ||
end | ||
|
||
# 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: passing pointer not list to rec statck so O(n) | ||
def reverse | ||
raise NotImplementedError | ||
return if !@head | ||
curr = @head | ||
@tail = curr | ||
reverse_links(curr) | ||
end | ||
|
||
def reverse_links(curr) | ||
if !curr.next | ||
@head = curr | ||
return | ||
end | ||
reverse_links(curr.next) | ||
curr.next.next = curr | ||
curr.next = nil | ||
return | ||
end | ||
|
||
## Advanced Exercises | ||
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def find_middle_value | ||
raise NotImplementedError | ||
return if !@head | ||
fast,slow = @head, @head | ||
while fast.next && fast.next.next | ||
fast = fast.next.next | ||
slow = slow.next | ||
end | ||
return slow.data | ||
end | ||
|
||
# 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(1) | ||
def find_nth_from_end(n) | ||
raise NotImplementedError | ||
target_node = node_at_index(length() - n - 1) | ||
return if !target_node | ||
return target_node.data | ||
end | ||
|
||
# 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 | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def has_cycle | ||
raise NotImplementedError | ||
return false if !@head | ||
fast, slow = @head, @head | ||
while fast.next.next | ||
fast = fast.next.next | ||
slow = slow.next | ||
if fast == slow | ||
return true | ||
end | ||
end | ||
return false | ||
end | ||
|
||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def get_first | ||
raise NotImplementedError | ||
return @head.data if @head | ||
return | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def add_last(value) | ||
raise NotImplementedError | ||
new_node = Node.new(value) | ||
if @tail | ||
@tail.next = new_node | ||
@tail = new_node | ||
else | ||
@head = new_node | ||
@tail = new_node | ||
end | ||
@length += 1 | ||
return | ||
end | ||
|
||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def get_last | ||
raise NotImplementedError | ||
return if !@tail | ||
return @tail.data | ||
end | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def insert_ascending(value) | ||
raise NotImplementedError | ||
new_node = Node.new(value) | ||
if !@head || value <= @head.data | ||
new_node.next = @head | ||
@head = new_node | ||
@tail = new_node | ||
@length += 1 | ||
return | ||
end | ||
curr = @head | ||
while curr.next && curr.next.data <= value | ||
curr = curr.next | ||
end | ||
temp = curr.next | ||
@tail = new_node if !curr.next | ||
curr.next = new_node | ||
new_node.next = temp | ||
@length += 1 | ||
return | ||
end | ||
|
||
# Helper method for tests | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Smart!