Skip to content

completed #38

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,74 @@ def initialize
@head = nil
end

# Time complexity - ?
# Space complexity - ?
# Time complexity - 0(1)
# Space complexity - 0(1)
def add_first(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


@head = Node.new(data, @head)
end

# Time complexity - ?
# Space complexity - ?
# Time complexity - 0(1)
# Space complexity - 0(1)
def get_first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍



if head
return head.data
else
return nil
end
end

# Time complexity - ?
# Space complexity - ?
# Time complexity - 0(n)
# Space complexity - 0(1)
def length

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return 0

current = head
length = 0
while current != nil
length += 1
current = current.next
end
return length
end

# Time complexity - ?
# Time complexity - 0(n)
# Space complexity - ?
def add_last(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


end
current = head
if current == nil
self.add_first(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good re-use of a method

return
end
while current.next != nil
current = current.next
end
current.next = Node.new(data)
end

# Time complexity - ?
# Space complexity - ?
def get_last
current = head

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if head is nil?

Suggested change
current = head
return nil if head.nil?
current = head

while current.next != nil
current = current.next
end
return current.data
end


end

# Time complexity - ?
# Space complexity - ?
def get_at_index(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about if the index is greater than the length of the list?

current = head
count = 0

return nil if current.nil?
while count != index
count += 1
current = current.next
end
return current.data
end
end
end