-
Notifications
You must be signed in to change notification settings - Fork 48
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
Earth - Emily #27
base: master
Are you sure you want to change the base?
Earth - Emily #27
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 |
---|---|---|
@@ -1,45 +1,83 @@ | ||
require_relative 'node' | ||
|
||
class LinkedList | ||
attr_reader :head | ||
attr_reader :head, :tail | ||
|
||
def initialize | ||
@head = nil | ||
@tail = tail | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def add_first(data) | ||
Comment on lines
+11
to
13
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. 👍 |
||
first = Node.new(data) | ||
|
||
if @head | ||
first.next = @head | ||
end | ||
|
||
@head = first | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def get_first | ||
Comment on lines
+23
to
25
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. 👍 |
||
if @head.nil? | ||
return nil | ||
end | ||
|
||
return @head.data | ||
|
||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def length | ||
Comment on lines
+34
to
36
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. How is the time complexity O(1)? You have a loop. 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. ah! yes, i changed this and just forgot to resubmit. it would be O(n) |
||
return 0 | ||
count = 0 | ||
current = @head | ||
|
||
while current != nil | ||
count += 1 | ||
current = current.next | ||
end | ||
|
||
return count | ||
|
||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def add_last(data) | ||
Comment on lines
+49
to
51
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. 👍 |
||
last = Node.new(data) | ||
|
||
@tail ? @tail.next = last : @head = last | ||
@tail = last | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def get_last | ||
Comment on lines
+58
to
60
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. 👍 |
||
if @tail.nil? | ||
return nil | ||
end | ||
|
||
return @tail.data | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(n) | ||
# Space complexity - O(n) | ||
def get_at_index(index) | ||
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 what if length < index |
||
return @head.data if index.zero? | ||
current_index = 0 | ||
current = @head | ||
|
||
until current.nil? | ||
current = current.next | ||
current_index += 1 | ||
|
||
return current.data if current_index == index | ||
end | ||
return nil | ||
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.
Nice having a tail pointer