## DEV Community is a community of 704,743 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.  Aya Bouchiha
Full stack web developer

Hi, on this beautiful day, we are going to talk about Linked List, we will cover and answer these Questions :

## Definition of a Linked List

A linked list is a linear data structure, in which the elements are linked using pointers, additionally, they are not stored at contiguous memory locations. A Linked List consists of Nodes that contain value ( data ) and a pointer to the next node in the chain. The head pointer points to the first node if the list is not empty, and the last element of the list points to null.

• Dynamic Size
• adding and deleting easily nodes without the need for displacement which is an expensive operation in an array

## Negatives of using Linked List

• Extra memory space for nodes' pointers
• We have to access elements sequentially starting from the first node to the wanted node.

O(n)

## Time Complexity of a singly Linked List

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

## Implementation of a Singly Linked List using Javascript from travesy media github

``````class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}

// Create/Get/Remove Nodes From Linked List
constructor() {
this.size = 0;
}

// Insert first node
insertFirst(data) {
this.size++;
}

// Insert last node
insertLast(data) {
let node = new Node(data);
let current;

} else {

while (current.next) {
current = current.next;
}

current.next = node;
}

this.size++;
}

// Insert at index
insertAt(data, index) {
//  If index is out of range
if (index > 0 && index > this.size) {
return;
}

// If first index
if (index === 0) {
this.insertFirst(data);
return;
}

const node = new Node(data);
let current, previous;

// Set current to first
let count = 0;

while (count < index) {
previous = current; // Node before index
count++;
current = current.next; // Node after index
}

node.next = current;
previous.next = node;

this.size++;
}

// Get at index
getAt(index) {
let count = 0;

while (current) {
if (count == index) {
console.log(current.data);
}
count++;
current = current.next;
}

return null;
}

// Remove at index
removeAt(index) {
if (index > 0 && index > this.size) {
return;
}

let previous;
let count = 0;

// Remove first
if (index === 0) {
} else {
while (count < index) {
count++;
previous = current;
current = current.next;
}

previous.next = current.next;
}

this.size--;
}

// Clear list
clearList() {
this.size = 0;
}

// Print list data
printListData() {

while (current) {
console.log(current.data);
current = current.next;
}
}
}
``````