From 98c29bdca0b43642f511d3818307a57a8871ca0d Mon Sep 17 00:00:00 2001 From: Kelsey Krippaehne Date: Sun, 8 Dec 2019 12:45:52 -0800 Subject: [PATCH 1/2] Added working methods --- lib/linked_list.rb | 56 +++++++++++++++++++++++++++++++++++----- test/linked_list_test.rb | 8 +++--- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9e97557..c54fc33 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -10,36 +10,80 @@ def initialize # Time complexity - ? # Space complexity - ? def add_first(data) - + node = Node.new(data, @head) + @head = node + return @head.data end # Time complexity - ? # Space complexity - ? def get_first - + return @head.data if @head end # Time complexity - ? # Space complexity - ? def length - return 0 + count = 0 + if @head + next_node = @head.next + count += 1 + while next_node != nil + count += 1 + next_node = next_node.next + end + end + return count end # Time complexity - ? # Space complexity - ? def add_last(data) - + if @head + if @head.next + next_node = @head.next + until next_node.next == nil + next_node = next_node.next + end + next_node.next = Node.new(data) + else + @head.next = Node.new(data) + end + else + @head = Node.new(data) + end end # Time complexity - ? # Space complexity - ? def get_last - + if @head.next + last_node = @head.next + while last_node.next != nil + last_node = last_node.next + end + return last_node.data + elsif @head + return @head.data + else + return nil + end end # Time complexity - ? # Space complexity - ? def get_at_index(index) - + if @head.nil? + return nil + else + index = index + node_index = 0 + next_node = @head + until node_index == index || next_node == nil + node_index += 1 + next_node = next_node.next + end + return next_node.data + end end end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 2a805c7..fac4c41 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -21,7 +21,7 @@ end end - xdescribe 'add_first & get_first' do + describe 'add_first & get_first' do it 'can add values to an empty list' do # Act @list.add_first(3) @@ -51,7 +51,7 @@ end end - xdescribe "length" do + describe "length" do it "will return 0 for an empty list" do expect(@list.length).must_equal 0 end @@ -66,7 +66,7 @@ end end - xdescribe "addLast & getLast" do + describe "addLast & getLast" do it "will add to the front if the list is empty" do @list.add_last(1) expect(@list.get_first).must_equal 1 @@ -91,7 +91,7 @@ end - xdescribe 'get_at_index' do + describe 'get_at_index' do it 'returns nil if the index is outside the bounds of the list' do expect(@list.get_at_index(3)).must_be_nil end From 3387bd12588d9f7342da3412b841e9a7cfe1d7b1 Mon Sep 17 00:00:00 2001 From: Kelsey Krippaehne Date: Mon, 9 Dec 2019 14:46:29 -0800 Subject: [PATCH 2/2] Added time & space complexity --- lib/linked_list.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index c54fc33..4ecedee 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -7,22 +7,22 @@ def initialize @head = nil end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(1) + # Space complexity - O(1), thanks to the power of linked lists we are not making any new data structures, just one new node. def add_first(data) node = Node.new(data, @head) @head = node return @head.data end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(1) + # Space complexity - O(1) def get_first return @head.data if @head end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n), where n is the length of the list + # Space complexity - O(1) def length count = 0 if @head @@ -36,8 +36,8 @@ def length return count end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) + # Space complexity - O(1) def add_last(data) if @head if @head.next @@ -54,8 +54,8 @@ def add_last(data) end end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n) + # Space complexity - O(1) def get_last if @head.next last_node = @head.next @@ -70,8 +70,8 @@ def get_last end end - # Time complexity - ? - # Space complexity - ? + # Time complexity - O(n), where `n` = index + # Space complexity - O(1) def get_at_index(index) if @head.nil? return nil