-
Notifications
You must be signed in to change notification settings - Fork 48
Space - Charlotte - Linked List #34
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
base: master
Are you sure you want to change the base?
Changes from all commits
9210c2d
e0981b2
2060c03
d081d35
d5d414c
c228605
fdae59d
71c5a1b
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 |
|---|---|---|
|
|
@@ -18,144 +18,222 @@ 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: ? | ||
| def add_first(value) | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
| # Time Complexity: 0(1) | ||
| # Space Complexity: 0(1) | ||
|
|
||
| def add_first(value) | ||
| @head = Node.new(value, @head) | ||
| return @head | ||
| end | ||
| # new_node = Node.new(value) | ||
| # new_node.next = @head | ||
| # @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: ? | ||
| # Time Complexity:0(n) with n being the length of the list | ||
| # Space Complexity:0(n) | ||
| def search(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. 👍 , but the space complexity is O(1) as you're not creating a new list here. |
||
| raise NotImplementedError | ||
| return false if @head.nil? | ||
|
|
||
| current = @head | ||
| while (current.next != nil && current.data != value) do | ||
| current = current.next | ||
| if current.data != value | ||
| return false | ||
| end | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max | ||
|
Comment on lines
50
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. 👍 |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| current = @head | ||
| max = current.data | ||
| while (current.next != nil) | ||
| current = current.next | ||
| if current.data > max | ||
| max = current.data | ||
| end | ||
| 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: 0(n) with n being the length of the list | ||
| # Space Complexity: 0(n) | ||
| def find_min | ||
|
Comment on lines
+68
to
70
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. 👍 , but the space complexity is O(1) as you're not creating a new list here. |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| current = @head | ||
| min = current.data | ||
| while (current.next != nil) | ||
| current = current.next | ||
| if current.data < min | ||
| min = current.data | ||
| end | ||
| end | ||
| return min | ||
| end | ||
|
|
||
|
|
||
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: 0(n) with n being the length | ||
| # Space Complexity: 0(n) | ||
| def length | ||
|
Comment on lines
+86
to
88
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. 👍 , but the space complexity is O(1) as you're not creating a new list here. |
||
| raise NotImplementedError | ||
| current_node = @head | ||
| length = 0 | ||
| return 0 if @head.nil? | ||
|
|
||
| while (!current_node.nil?) | ||
| current_node = current_node.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: 0(n) with n being the length of the list | ||
| # Space Complexity: 0(n) | ||
| def get_at_index(index) | ||
|
Comment on lines
+104
to
106
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. 👍 , but the space complexity is O(1) as you're not creating a new list here. |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
| current = @head | ||
| index.times do | ||
| return nil if current.next.nil? | ||
| current = current.next | ||
| end | ||
| return current.data | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: 0(n) with n being the length of the list | ||
| # Space Complexity: 0(n) | ||
| def visit | ||
|
Comment on lines
+117
to
119
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. 👍 , but the space complexity is O(1) as you're not creating a new list here. |
||
| raise NotImplementedError | ||
| current = @head | ||
| if current = current.data | ||
| return current.data | ||
| end | ||
| end | ||
|
|
||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Time Complexity: 0(n) with n being the length of the list | ||
| # Space Complexity: ? | ||
| def delete(value) | ||
| raise NotImplementedError | ||
| # if head is nil - return | ||
| if @head.nil? | ||
| return | ||
| end | ||
| # delete head if needed | ||
| if @head.data == value | ||
| @head = @head.next | ||
| return | ||
| end | ||
|
|
||
| current_node = @head | ||
| while (current_node.next != nil ) | ||
| if current_node.next.data == value | ||
| current_node.next = current_node.next.next | ||
| return | ||
| end | ||
| current_node = current_node.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: 0(n) n being the length of the list | ||
| # Space Complexity: 0(n) | ||
| def reverse | ||
|
Comment on lines
+153
to
155
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. 👍 , but the space complexity is O(1) as you're not creating a new list here. |
||
| raise NotImplementedError | ||
| return if @head.nil? | ||
| current = @head | ||
| previous_node = nil | ||
| while (!current.nil?) | ||
| temp = current.next | ||
| current.next = previous_node | ||
| previous_node = current | ||
| current = temp | ||
| end | ||
| @head = previous_node | ||
| end | ||
| 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 | ||
| # def find_middle_value | ||
| # 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 | ||
| # def find_nth_from_end(n) | ||
| # 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 | ||
| # def has_cycle | ||
| # 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 | ||
| # def get_first | ||
|
|
||
| # 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 | ||
| # def add_last(value) | ||
|
|
||
| # 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 | ||
| # def get_last | ||
|
|
||
| # 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 | ||
| # def insert_ascending(value) | ||
|
|
||
| # 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 | ||
| # def create_cycle | ||
| # return if @head == nil # don't do anything if the linked list is empty | ||
|
|
||
| # 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 | ||
| # current = @head | ||
| # while current.next != nil | ||
| # current = current.next | ||
| # end | ||
|
|
||
| # current.next = @head # make the last node link to first node | ||
| # end | ||
| # end | ||
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.
👍