-
Notifications
You must be signed in to change notification settings - Fork 48
Liv - Space #36
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?
Liv - Space #36
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,158 @@ 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: o1 | ||
| # Space Complexity: o1 | ||
| 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: o1 | ||
| # Space Complexity: o1 | ||
| 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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
|
|
||
| while current | ||
| if current.data == value | ||
| return true | ||
| end | ||
| 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 | ||
| #Time Complexity: on | ||
| # Space Complexity: o1 | ||
| def find_max | ||
|
Comment on lines
+46
to
48
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 = @head.data | ||
|
|
||
| while current | ||
| if current.data > max | ||
| max = current.data | ||
| end | ||
| 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
+66
to
68
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 | ||
| min = @head.data | ||
|
|
||
| while current | ||
| 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: on | ||
| # Space Complexity: o1 | ||
| 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. 👍 |
||
| raise NotImplementedError | ||
| current = @head | ||
| count = 0 | ||
|
|
||
| while 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: on | ||
| # Space Complexity: o1 | ||
| def get_at_index(index) | ||
|
Comment on lines
+103
to
105
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 | ||
| current = @head | ||
| count = 0 | ||
|
|
||
| until current.nil? | ||
| return current.data if count == index | ||
| current = current.next | ||
| count += 1 | ||
| end | ||
|
|
||
| return nil | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def visit | ||
|
Comment on lines
119
to
121
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 and space complexity ❓ |
||
| raise NotImplementedError | ||
| print @head.data if @head.next == nil | ||
|
|
||
| current = @head | ||
|
|
||
| while current | ||
| print current.data | ||
| current = current.next | ||
| end | ||
| end | ||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: on | ||
| # Space Complexity: o1 | ||
| def delete(value) | ||
|
Comment on lines
+133
to
135
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? | ||
|
|
||
| if @head.data == value | ||
| @head = @head.next | ||
| return | ||
| end | ||
|
|
||
| previous = @head | ||
| current = @head.next | ||
|
|
||
| until current.nil? || current.data == value | ||
| previous = current | ||
| current = current.next | ||
| end | ||
|
|
||
| previous.next = current.next | ||
| 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: on | ||
| # Space Complexity: o1 | ||
| def reverse | ||
|
Comment on lines
+156
to
158
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 | ||
| subsequent = nil | ||
| previous = nil | ||
|
|
||
| while current | ||
| subsequent = current.next | ||
| current.next = previous | ||
| previous = current | ||
| current = subsequent | ||
| end | ||
|
|
||
| @head = previous | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -91,7 +178,9 @@ def reverse | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def find_middle_value | ||
|
Comment on lines
178
to
180
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 | ||
| length = self.length | ||
| mid = length/2 | ||
| return get_at_index(mid) | ||
| end | ||
|
|
||
| # find the nth node from the end and return its value | ||
|
|
@@ -118,21 +207,38 @@ def has_cycle | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_first | ||
|
Comment on lines
207
to
209
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? | ||
| return @head.data | ||
| end | ||
|
|
||
| # method that inserts a given value as a new last node in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def add_last(value) | ||
|
Comment on lines
214
to
217
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 | ||
|
|
||
| return self.add_first(data) if current == nil | ||
| while current.next != nil | ||
| current = current.next | ||
| end | ||
|
|
||
| current.next = Node.new(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 | ||
|
Comment on lines
230
to
234
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. 👍 |
||
| current = @head | ||
|
|
||
| while current.next != nil | ||
| current = current.next | ||
| end | ||
|
|
||
| return current.data | ||
| raise NotImplementedError | ||
| 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.
👍