# A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow. Linked List Insertion method 

**URL:** <https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-by-jay-wengrow-linked-list-insertion-method/94363>\
**Category:** PragProg Customers\
**Tags:** book-a-common-sense-guide-to-data-structures-and-algorithms-second-edition, book, errata\
**Created:** [31 January 2023 15:55 UTC](https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-by-jay-wengrow-linked-list-insertion-method/94363 "2023-01-31T15:55:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hayashi](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/h/439d5e/32.png) [@hayashi](https://forum.devtalk.com/u/hayashi)\
**Post date:** [31 January 2023 15:55 UTC](https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-by-jay-wengrow-linked-list-insertion-method/94363/1 "2023-01-31T15:55:17Z")

</div>

### @jaywengrow

A Common-Sense Guide to Data Structures and Algorithms - insertion at wrong index (page 140)

I tested the example of a linked list insertion method on page 140 (2017 edition):

```auto
class Node
  attr_accessor :data, :next_node
  def initialize(data) 
    @data = data
  end 
end

class LinkedList
  attr_accessor :first_node
  # rest of code omitted here...

  def insert_at_index(index, value) 
    current_node = first_node 
    current_index = 0

    # First, we find the index immediately before where the new node will go:
     while current_index < index do
       current_node = current_node.next_node
       current_index += 1
     end

     # We create the new node:
     new_node = Node.new(value)
     new_node.next_node = current_node.next_node
     # We modify the link of the previous node to point to our new node:
     current_node.next_node = new_node
  end
end

```

The method seems to be inserting a new node after the provided index, e.g.

```auto

node_1 = Node.new("once") 
node_2 = Node.new("upon") 
node_1.next_node = node_2
node_3 = Node.new("a") 
node_2.next_node = node_3
node_4 = Node.new("time") 
node_3.next_node = node_4

list = LinkedList.new(node_1) # 0. "once" 1. "upon" 2. "a" 3. "time"
list.insert_at_index(3, "beautiful") # 0 "once" 1. "upon" 2. "a" 3. "time" 4. "beautiful"

```

Shouldn’t the `while` loop be `while current_index < index-1` instead?

Thank you for the brilliant book anyway @jaywengrow . It’s a great learning resource!

---

<div class="post-metadata">

**Author:** ![jaywengrow](https://forum.devtalk.com/user_avatar/forum.devtalk.com/jaywengrow/32/527_2.png) [@jaywengrow](https://forum.devtalk.com/u/jaywengrow)\
**Post date:** [5 March 2023 20:08 UTC](https://forum.devtalk.com/t/a-common-sense-guide-to-data-structures-and-algorithms-by-jay-wengrow-linked-list-insertion-method/94363/2 "2023-03-05T20:08:48Z")

</div>

Thanks for this! You are correct, and this has indeed been fixed in the Second Edition of the book.
