shea-linked_list 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c599638b3c442a32a9b441cba92a10587a79344e5c04d3b37f45f7cce016a79
4
- data.tar.gz: 013a5eb551da5dbadac20986500e32b9b7078dc2d9102f6ed382719e9d69e680
3
+ metadata.gz: 31c91530bafbcd86744563af3d65fa7a37cd19607ae36cfd584ce6c65d043c39
4
+ data.tar.gz: 58cf583c26a51cd2a6e22e49b45544834e508ab2e6390f64efb2b61ae518083c
5
5
  SHA512:
6
- metadata.gz: e937c9f50f847f004a8b3bf68faccd5dcd193dbf7315e4cdc07257ba74529f686ca7247ac22ed9838f4c9a79535591ad26c99f6e7aa69ef70d420e921f160dfd
7
- data.tar.gz: e5c314c4bdd465ac47d03d936401fb684bc35de991cafd0ccaf4921bdecd49dfbaf17b528c146b5648972959fbf314678d8824802da336a0d392d63d81772549
6
+ metadata.gz: 12a12886383e78226faf5b31ca9b84dceb232b1a7773861da4c993e95c8da11f6f2afcfe6f324a2c442afb30994dddda30a3750882db623f8ccd9fad9750ba25
7
+ data.tar.gz: f5a5dfc75b4b7b28e2a83e62d9331280983196aa468353eeeabb30cf116cfc0d4700e558b67b13237daf40ea750e3687d40833b1a59520c388809d0851762570
@@ -42,6 +42,8 @@ class LinkedList
42
42
 
43
43
  # returns the node at the given index
44
44
  def at(index)
45
+ return nil if index.negative? || index > size - 1
46
+
45
47
  i = 0
46
48
  node = @head
47
49
  while i < index
@@ -83,17 +85,26 @@ class LinkedList
83
85
 
84
86
  # inserts a new node with the provided value at the given index
85
87
  def insert_at(value, index)
86
- next_node = at(index)
87
- node = Node.new(value, next_node)
88
- prev_node = at(index - 1)
89
- prev_node.next_node = node
88
+ case index
89
+ when 0 then prepend(value)
90
+ when size - 1 then append(value)
91
+ else
92
+ next_node = at(index)
93
+ node = Node.new(value, next_node)
94
+ prev_node = at(index - 1)
95
+ prev_node.next_node = node
96
+ end
90
97
  end
91
98
 
92
99
  # removes the node at the given index
93
100
  def remove_at(index)
94
101
  prev_node = at(index - 1)
95
102
  next_node = at(index + 1)
96
- prev_node.next_node = next_node
103
+ if index.zero?
104
+ @head = next_node
105
+ else
106
+ prev_node.next_node = next_node
107
+ end
97
108
  end
98
109
 
99
110
  # the format should be: ( value ) -> ( value ) -> ( value ) -> nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shea-linked_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shea Cronin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-19 00:00:00.000000000 Z
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: