-
Notifications
You must be signed in to change notification settings - Fork 48
Space - Katie #40
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?
Space - Katie #40
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 |
|---|---|---|
|
|
@@ -12,46 +12,99 @@ def initialize(value, next_node = nil) | |
|
|
||
| # Defines the singly linked list | ||
| class LinkedList | ||
| attr_reader :head | ||
|
|
||
| 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: ? | ||
| # Time Complexity: O(1) | ||
| # Space Complexity: O(1) | ||
| def add_first(value) | ||
| raise NotImplementedError | ||
| new_node = Node.new(value) | ||
| if @head.nil? | ||
| @head = new_node | ||
| else | ||
| new_node.next = @head | ||
| @head = new_node | ||
| end | ||
|
|
||
| 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) | ||
|
Comment on lines
+38
to
40
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. 👍 , nice use of the |
||
| raise NotImplementedError | ||
| if @head.nil? && !value.nil? | ||
| return false | ||
| else | ||
| current_node = @head | ||
| until current_node.nil? | ||
| return true if current_node.data == value | ||
| current_node = current_node.next | ||
| end | ||
| return false | ||
| end | ||
| 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
53
to
55
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 | ||
| if @head.nil? | ||
| return nil | ||
| else | ||
| max = @head.data | ||
| current_node = @head | ||
| until current_node.next.nil? | ||
| current_node = current_node.next | ||
|
|
||
| if current_node.data > max | ||
| max = current_node.data | ||
| end | ||
| end | ||
| return max | ||
| end | ||
| 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 | ||
|
Comment on lines
+74
to
76
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 | ||
| if @head.nil? | ||
| return nil | ||
| else | ||
| min = @head.data | ||
| current_node = @head | ||
| until current_node.next.nil? | ||
| current_node = current_node.next | ||
| if current_node.data < min | ||
| min = current_node.data | ||
| end | ||
| end | ||
| return min | ||
| end | ||
| 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 | ||
|
Comment on lines
+94
to
96
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 | ||
| if @head.nil? | ||
| return 0 | ||
| else | ||
| count = 1 | ||
| current_node = @head | ||
| until current_node.next.nil? | ||
| current_node = current_node.next | ||
| count += 1 | ||
| end | ||
| return count | ||
| end | ||
| end | ||
|
|
||
| # method that returns the value at a given index in the linked list | ||
|
|
@@ -60,29 +113,98 @@ def length | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_at_index(index) | ||
|
Comment on lines
113
to
115
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 time/space complexity? |
||
| raise NotImplementedError | ||
| if @head.nil? | ||
| return nil | ||
| else | ||
| count = 0 | ||
| indexed_value = @head | ||
| until count == index || indexed_value.next.nil? | ||
| count += 1 | ||
| indexed_value = indexed_value.next | ||
| end | ||
| if count != index | ||
| return nil | ||
| end | ||
| return indexed_value.data | ||
| 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 | ||
|
Comment on lines
+134
to
136
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 | ||
| if @head.nil? | ||
| return nil | ||
| else | ||
| puts(@head.data) | ||
| current_node = @head | ||
| until current_node.next.nil? | ||
| current_node = current_node.next | ||
| puts(current_node.data) | ||
| end | ||
| return | ||
| end | ||
| end | ||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def delete(value) | ||
|
Comment on lines
+151
to
153
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 | ||
| if @head.nil? && !value.nil? | ||
| return nil | ||
| else | ||
| previous_node = nil | ||
| current_node = nil | ||
| if @head.data == value | ||
| @head = @head.next | ||
| return | ||
| else | ||
| previous_node = @head | ||
| current_node = @head.next | ||
| end | ||
|
|
||
| until current_node.nil? | ||
|
|
||
| if current_node.data == value | ||
| previous_node.next = current_node.next | ||
| return | ||
| end | ||
| previous_node = previous_node.next | ||
| current_node = current_node.next | ||
| end | ||
| return | ||
| 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) | ||
| # Space Complexity: O(n) | ||
| def reverse | ||
|
Comment on lines
+182
to
184
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. 👍 , interesting how you made a hash of all the nodes then went through that hash again reconstructing the list. Could you also do it with O(1) space complexity without having to make that hash. |
||
| raise NotImplementedError | ||
| if @head.nil? | ||
| return nil | ||
| else | ||
| all_nodes = {} | ||
| current_node = @head | ||
| current_count = 0 | ||
| total_length = 1 | ||
| all_nodes[current_count] = current_node | ||
| until current_node.next.nil? | ||
| current_count += 1 | ||
| total_length += 1 | ||
| current_node = current_node.next | ||
| all_nodes[current_count] = current_node | ||
| end | ||
|
|
||
| @head = all_nodes[total_length-1] | ||
| current_node = @head | ||
| (total_length-1).times do |x| | ||
| new_index_to_add = total_length-(x+2) | ||
| current_node.next = all_nodes[new_index_to_add] | ||
| current_node = current_node.next | ||
| end | ||
| 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.
👍