Skip to content

Commit

Permalink
fix(linked-list): insert in the middle bug
Browse files Browse the repository at this point in the history
When inserting an item on the middle of a linked list one reference was not being updated properly.

Fixed #8
  • Loading branch information
amejiarosario committed Apr 19, 2019
1 parent 9284acb commit f8bd4fd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/data-structures/linked-lists/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class LinkedList {
newNode.next = current; // <5>

current.previous.next = newNode; // <6>
if (current.next) { current.next.previous = newNode; } // <7>
current.previous = newNode; // <7>
this.size += 1;
return newNode;
}
Expand Down
4 changes: 4 additions & 0 deletions src/data-structures/linked-lists/linked-list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ describe('LinkedList Test', () => {
it('should insert at the middle', () => {
const newNode = linkedList.add('middle', 1);
expect(newNode.value).toBe('middle');
// checking the 4 surrounding links were updated
expect(newNode.next.value).toBe('found');
expect(newNode.previous.value).toBe(0);
expect(linkedList.get(0).next.value).toBe('middle');
expect(linkedList.get(2).previous.value).toBe('middle');

expect(linkedList.size).toBe(3);
expect(linkedList.first.value).toBe(0);
});
Expand Down

0 comments on commit f8bd4fd

Please sign in to comment.