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
Ports - Sopheary #33
Open
sophearychiv
wants to merge
6
commits into
Ada-C11:master
Choose a base branch
from
sophearychiv: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
Ports - Sopheary #33
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -18,132 +18,265 @@ def initialize | |
|
||
# 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) | ||
new_node.next = @head | ||
@head = new_node | ||
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) | ||
# Space Complexity: O(1) | ||
def search(value) | ||
raise NotImplementedError | ||
return false if @head.nil? | ||
return true if @head.data = value | ||
current = @head | ||
until current.data.nil? | ||
return true if current.data = value | ||
current = current.next | ||
end | ||
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) | ||
# Space Complexity: O(1) | ||
def find_max | ||
raise NotImplementedError | ||
return nil if @head.nil? | ||
current = @head | ||
max = current.data | ||
until current.nil? | ||
max = current.data if current.data > max | ||
current = current.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) | ||
# Space Complexity: O(1) | ||
def find_min | ||
raise NotImplementedError | ||
return nil if @head.nil? | ||
current = @head | ||
min = current.data | ||
until current.nil? | ||
if current.data < min | ||
min = current.data | ||
end | ||
current = current.next | ||
end | ||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def length | ||
raise NotImplementedError | ||
return 0 if @head.nil? | ||
length = 0 | ||
current = @head | ||
until current.nil? | ||
current = current.next | ||
length += 1 | ||
end | ||
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) | ||
# Space Complexity: O(1) | ||
def get_at_index(index) | ||
raise NotImplementedError | ||
return nil if length <= index | ||
current = @head | ||
count = 0 | ||
until current.nil? | ||
return current.data if count == index | ||
current = current.next | ||
count += 1 | ||
end | ||
end | ||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def visit | ||
raise NotImplementedError | ||
current = @head | ||
until current.nil? | ||
puts current.data | ||
current = current.next | ||
end | ||
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.nil? | ||
current = @head | ||
|
||
if current.data == value | ||
@head = current.next | ||
return | ||
end | ||
|
||
prev = current | ||
until current.nil? | ||
if current.data == value | ||
prev.next = current.next | ||
else | ||
prev = current | ||
end | ||
current = current.next | ||
end | ||
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) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def reverse | ||
raise NotImplementedError | ||
return nil if @head.nil? | ||
prev = nil | ||
current = @head | ||
until current.nil? | ||
temp = current.next | ||
current.next = prev | ||
prev = current | ||
current = temp | ||
end | ||
|
||
@head = prev | ||
|
||
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 nil if @head.nil? | ||
current = @head | ||
if length % 2 == 0 | ||
return nil | ||
else | ||
middle_index = length / 2 | ||
end | ||
index = 0 | ||
until current.nil? | ||
return current.data if index == middle_index | ||
current = current.next | ||
index += 1 | ||
end | ||
|
||
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 | ||
return nil if @head.nil? | ||
current = @head | ||
index = 0 | ||
until current.nil? | ||
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 if n is larger than the length of the list? |
||
if index == length - n - 1 | ||
return current.data | ||
end | ||
index += 1 | ||
current = current.next | ||
end | ||
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 nil if @head.nil? | ||
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. 👍 |
||
slow_p = @head | ||
fast_p = @head | ||
while slow_p != nil && fast_p != nil and fast_p.next != nil | ||
slow_p = slow_p.next | ||
fast_p = fast_p.next.next | ||
if slow_p == fast_p | ||
return true | ||
end | ||
return false | ||
end | ||
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 nil if @head.nil? | ||
return @head.data | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: | ||
# Space Complexity | ||
# Time Complexity: O(n) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def add_last(value) | ||
raise NotImplementedError | ||
new_node = Node.new(value) | ||
if @head.nil? | ||
@head = new_node | ||
return | ||
end | ||
current = @head | ||
until current.next.nil? | ||
current = current.next | ||
end | ||
current.next = new_node | ||
|
||
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(n) where n is the number of nodes | ||
# Space Complexity: O(1) | ||
def get_last | ||
raise NotImplementedError | ||
return nil if @head.nil? | ||
current = @head | ||
until current.next.nil? | ||
current = current.next | ||
end | ||
return current.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) | ||
@head = new_node if @head.nil? | ||
current = @head | ||
if current.data > new_node.data | ||
temp = @head | ||
@head = new_node | ||
new_node.next = temp | ||
end | ||
until current.nil? | ||
if current.data <= value && current.next.data > value | ||
temp = current.next | ||
current.next = new_node | ||
new_node.next = temp | ||
end | ||
current = current.next | ||
end | ||
end | ||
|
||
# Helper method for tests | ||
|
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.
Is this needed? Wouldn't the loop catch this as well?