-
Notifications
You must be signed in to change notification settings - Fork 48
Time - Lola #41
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?
Time - Lola #41
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 |
|---|---|---|
|
|
@@ -18,71 +18,145 @@ 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 | ||
| @head = Node.new(value, @head) | ||
| 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(1) | ||
| # Space Complexity: O(1) | ||
| def search(value) | ||
|
Comment on lines
+29
to
31
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 why do you think the time complexity is O(1)? You're looping through the list. |
||
| raise NotImplementedError | ||
| return false if @head.nil? | ||
| current = @head | ||
|
|
||
| until current == nil | ||
| return true if current.data == value | ||
| current = current.next | ||
| end | ||
| return false | ||
| 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
42
to
44
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? | ||
|
|
||
| max = @head.data | ||
| current = @head | ||
|
|
||
| while current | ||
| 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 | ||
|
Comment on lines
+59
to
61
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 | ||
| end | ||
| return nil if @head.nil? | ||
|
|
||
| min = @head.data | ||
| current = @head | ||
|
|
||
| while current | ||
| min = current.data if current.data < min | ||
| 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 | ||
|
Comment on lines
+75
to
77
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 0 if @head.nil? | ||
|
|
||
| count = 0 | ||
| current = @head | ||
|
|
||
| until current == nil | ||
| count += 1 | ||
| current = current.next | ||
| end | ||
| return 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: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def get_at_index(index) | ||
|
Comment on lines
+93
to
95
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 what if index >= length? |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| current = @head | ||
| index.times do | ||
| current = current.next | ||
| end | ||
| current.data | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def visit | ||
|
Comment on lines
+106
to
108
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 how is the space complexity O(n), what other data structure are you creating? |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| current = @head | ||
|
|
||
| until current.nil? | ||
| print 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) | ||
| # Space Complexity: O(n) | ||
| def delete(value) | ||
|
Comment on lines
+120
to
122
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. 👍 , why do you think the space complexity is O(n), what new data structure are you creating? |
||
| raise NotImplementedError | ||
| # return nil if @head.nil? | ||
|
|
||
| current = @head | ||
| previous = nil | ||
| return nil if current.nil? | ||
|
|
||
| until current.data == value | ||
| previous = current | ||
| current = current.next | ||
| end | ||
|
|
||
| if previous.nil? | ||
| @head = current.next | ||
| else | ||
| previous.next = 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) | ||
| # Space Complexity: O(1) | ||
| def reverse | ||
|
Comment on lines
+144
to
146
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 | ||
| previous = nil | ||
|
|
||
| until current.next.nil? | ||
| temp = current.next | ||
| current.next = previous | ||
| previous = current | ||
| current = temp | ||
| end | ||
| current.next = previous | ||
| @head = current | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -118,7 +192,11 @@ def has_cycle | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_first | ||
|
Comment on lines
192
to
194
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. 👍 , time/space complexity? |
||
| raise NotImplementedError | ||
| if @head.nil? | ||
| return nil | ||
| else | ||
| return @head.data | ||
| end | ||
| end | ||
|
|
||
| # method that inserts a given value as a new last node in the linked list | ||
|
|
||
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.
👍