Linked List
It is a common data structure found in all programming languages. A linked list is very similar to a normal array in Javascript, it just acts a little bit differently.
Here each element in the list is a separate object containing a link or a pointer to the next. There is no built-in method or function for linked lists in Javascript so one has to implement it. An example of a linked list is shown below.
["one", "two", "three", "four"]
Types of Linked Lists
There are three different types of linked lists:
- Singly Linked Lists: Each node contains only one pointer to the next node.
- Doubly Linked Lists: There are two pointers at each node, one to the next node and one to the previous node.
- Circular Linked Lists: A circular linked list forms a loop by having the last node pointing to the first node or any other node before it.
Add
The add
method is created here to add value to a linked list.
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor(head) {
this.head = head
}
append = (value) => {
const newNode = new Node(value)
let current = this.head
if (!this.head) {
this.head = newNode
return
}
while (current.next) {
current = current.next
}
current.next = newNode
}
}
Pop
Here, a pop
method is created to remove a value from the linked list.
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor(head) {
this.head = head
}
pop = () => {
let current = this.head
while (current.next.next) {
current = current.next
}
current.next = current.next.next
}
}
Prepend
Here, a prepend
method is created to add a value before the first child of the linked list.
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor(head) {
this.head = head
}
prepend = (value) => {
const newNode = new Node(value)
if (!this.head) {
this.head = newNode
}
else {
newNode.next = this.head
this.head = newNode
}
}
}
Shift
Here, the shift
method is created to remove the first element of the Linked List.
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor(head) {
this.head = head
}
shift = () => {
this.head = this.head.next
}
}