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
Mina #29
Open
minams
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
minams: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
Mina #29
Changes from all commits
Commits
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
|
||
# Defines a node in the singly linked list | ||
class Node | ||
attr_reader :data # allow external entities to read value but not write | ||
|
@@ -12,152 +13,176 @@ def initialize(value, next_node = nil) | |
|
||
# Defines the singly linked list | ||
class LinkedList | ||
def initialize | ||
@head = nil # keep the head private. Not accessible outside this class | ||
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 | ||
def add_first(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
# Time Complexity: | ||
# Space Complexity | ||
def search(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
def find_max | ||
raise NotImplementedError | ||
end | ||
def initialize | ||
@head = nil # keep the head private. Not accessible outside this class | ||
end | ||
|
||
# method to return the min value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
def find_min | ||
raise NotImplementedError | ||
# 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: O(1) constant because adding first node | ||
# Space Complexity: O(1) constant because adding one node | ||
def add_first(value) | ||
new_node = Node.new(value) | ||
if @head != nil | ||
new_node.next = @head | ||
end | ||
@head = new_node | ||
end | ||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
# Time Complexity: O(1) constant because only searching for value of head | ||
# Space Complexity: O(1) constant because no additional space is needed | ||
def get_first | ||
return nil if !@head | ||
return @head.data | ||
end | ||
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def length | ||
raise NotImplementedError | ||
end | ||
# method that returns the length of the singly linked list | ||
# Time Complexity: O(n) where n equals the length of the linked lisst | ||
# Space Complexity: O(n) where n equals the length of the linked list | ||
def length | ||
return 0 if !@head | ||
length_count = 0 | ||
current_position = @head | ||
until current_position == nil | ||
length_count += 1 | ||
current_position = current_position.next | ||
end | ||
return length_count | ||
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 | ||
def get_at_index(index) | ||
raise NotImplementedError | ||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: O(n) where n is the length of the linked list | ||
# Space Complexity: O(1) constant because adding one node | ||
def add_last(value) | ||
new_node = Node.new(value) | ||
if @head == nil | ||
@head = new_node | ||
return @head | ||
end | ||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def visit | ||
raise NotImplementedError | ||
current = @head | ||
until current.next == nil | ||
current = current.next | ||
end | ||
current.next = new_node | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: | ||
# Space Complexity | ||
def delete(value) | ||
raise NotImplementedError | ||
end | ||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
# Time Complexity: O(n) where n is the length of the linked list | ||
# Space Complexity: O(1) constant because only retrieving last value | ||
def get_last | ||
return nil if !@head | ||
current = @head | ||
while current.next != nil | ||
current = current.next | ||
end | ||
return current.data | ||
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 | ||
def reverse | ||
raise NotImplementedError | ||
end | ||
# method that returns the value at a given index in the linked list | ||
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. And a bunch of unimplemented methods. |
||
# index count starts at 0 | ||
# returns nil if there are fewer nodes in the linked list than the index value | ||
# Time Complexity: | ||
# Space Complexity: | ||
def get_at_index(index) | ||
raise NotImplementedError | ||
end | ||
|
||
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
# Time Complexity: | ||
# Space Complexity | ||
def search(value) | ||
raise NotImplementedError | ||
end | ||
|
||
## Advanced Exercises | ||
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def find_middle_value | ||
raise NotImplementedError | ||
end | ||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
def find_max | ||
raise NotImplementedError | ||
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 | ||
def find_nth_from_end(n) | ||
raise NotImplementedError | ||
end | ||
# method to return the min value in the linked list | ||
# returns the data value and not the node | ||
# Time Complexity: | ||
# Space Complexity | ||
def find_min | ||
raise NotImplementedError | ||
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 | ||
def has_cycle | ||
raise NotImplementedError | ||
end | ||
# method to print all the values in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def visit | ||
raise NotImplementedError | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: | ||
# Space Complexity | ||
def delete(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
# Time Complexity: | ||
# Space Complexity | ||
def get_first | ||
raise NotImplementedError | ||
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 | ||
def reverse | ||
raise NotImplementedError | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def add_last(value) | ||
raise NotImplementedError | ||
end | ||
## Advanced Exercises | ||
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
def find_middle_value | ||
raise NotImplementedError | ||
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 | ||
def get_last | ||
raise NotImplementedError | ||
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 | ||
def find_nth_from_end(n) | ||
raise NotImplementedError | ||
end | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
# Time Complexity: | ||
# Space Complexity | ||
def insert_ascending(value) | ||
raise NotImplementedError | ||
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 | ||
def has_cycle | ||
raise NotImplementedError | ||
end | ||
|
||
# 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 | ||
return if @head == nil # don't do anything if the linked list is empty | ||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
# Time Complexity: | ||
# Space Complexity | ||
def insert_ascending(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# navigate to last node | ||
current = @head | ||
while current.next != nil | ||
current = current.next | ||
end | ||
# 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 | ||
return if @head == nil # don't do anything if the linked list is empty | ||
|
||
current.next = @head # make the last node link to first node | ||
# navigate to last node | ||
current = @head | ||
while current.next != nil | ||
current = current.next | ||
end | ||
|
||
current.next = @head # make the last node link to first node | ||
end | ||
end |
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.
Space complexity O(n)? You aren't creating any additional data structure or using recursion.