# The Singly Linked List data structure

## #data-structures series

Posted on January 13, 2016

The #data-structures series is a collection of posts about reimplemented data structures in JavaScript.

If you are not familiar with data structures, a quick introduction and the full list of reimplemented data structures can be found in the introduction post of the series on data structures in JavaScript.

If you feel comfortable with the concept of each data structure and only want to see the code, have a look at the summary post of the series. It removes all explanations and contains only the JavaScript code for all data structures discussed in the series.

## Get the code on Github

Of course, all the code can also be found on Github in the repository data-structures-in-javascript.

# The Singly Linked List data structure

## Definition

A Singly Linked List is a linear collection of data elements, called nodes pointing to the next node by means of pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. From Wikipedia

Linked Lists are among the simplest and most common data structures because it allows for efficient insertion or removal of elements from any position in the sequence.

## Complexity

Average
Access Search Insertion Deletion
O(n) O(n) O(1) O(1)

To get a full overview of the time and space complexity of the Singly Linked List data structure, have a look to this excellent Big O cheat sheet.

## The code

``````function Node(data) {
this.data = data;
this.next = null;
}

this.tail = null;
this.numberOfValues = 0;
}

var node = new Node(data);
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this.numberOfValues++;
};
while(current) {
if(current.data === data) {
}
if(current === this.tail) {
this.tail = previous;
}
previous.next = current.next;
this.numberOfValues--;
} else {
previous = current;
}
current = current.next;
}
};
while(current) {
if(current.data === toNodeData) {
var node = new Node(data);
if(current === this.tail) {
this.tail.next = node;
this.tail = node;
} else {
node.next = current.next;
current.next = node;
}
this.numberOfValues++;
}
current = current.next;
}
};
while(current) {
if(fn) {
fn(current);
}
current = current.next;
}
};
return this.numberOfValues;
};
var string = '';
while(current) {
string += current.data + ' ';
current = current.next;
}
console.log(string.trim());
};

singlyLinkedList.print(); // => 1 2 3 4
console.log('length is 4:', singlyLinkedList.length()); // => 4
singlyLinkedList.print(); // => 1 2 4
singlyLinkedList.remove(9); // remove non existing value
singlyLinkedList.print(); // => 1 2 4
console.log('length is 1:', singlyLinkedList.length()); // => 1
singlyLinkedList.print(); // => 2 3 6
singlyLinkedList.print(); // => 2 3 4 6
singlyLinkedList.insertAfter(5, 9); // insertAfter a non existing node
singlyLinkedList.print(); // => 2 3 4 6