Linkedlist in JavaScript

Photo by JJ Ying on Unsplash


Believe me, all those aspirants looking for a job in any software industry.

LinkedList is going to be asked in all interviews you are going to attend. But why LinkedList?

Because, these data structures, not only LinkedList, there are many other data structures that would have been implemented in the product they develop in their company or the programming language they use and whatnot! But LinkedList is somewhat an interesting topic which teases our brain a little, and it is an easy to medium level question.

There is no software development without data structures. Data structures form the core of any programmable system. So why not spend some time and have fun with it.

And LinkedList in JavaScript is one of the fun parts of programming. You need not no about pointers, addresses, as JavaScript language is basically designed in such a way, where Objects(which is apt for storing multidimensional data of different types in JavaScript) are at the last will be referred by only its address. Even when you are assigning an Object variable to another variable, you are mapping only its address, no actual transition of data has occurred in that action. So, there are no referencing, dereferencing, and other craps and only concentrating on the core logic of the LinkedList.

Here, I'm sharing the self-explanatory code of some commonly asked functionalities of LinkedList like 
  • Adding an element.
  • Deleting an element.
  • Finding the middle of the element.
I hope it could help you in your interviews. All the very best. Feel free to comment down the doubts in this code.

// Linked list in javascript;
let head = null;


// Create a node
function createNode(value) {
let tempNode = {
value: value,
next: null
}
return tempNode
}

// Insert node at end
function insertNodeAtEnd() {
value = document.getElementById('insertValue').value;
let tempNode = createNode(value);
if(head == null) {
head = tempNode;
}
else {
let i;
for(i = head; i.next != null; i = i.next);
i.next = tempNode;
}
console.log('Value inserted');
return;
}

// Delete node at end
function deleteNodeAtEnd() {
let i = head;
if(i == null) {
return;
}
if(i.next) {
for(i = head; i.next != null; i = i.next);
}
i.next = null;
console.log('Value deleted');
}

// Display linkedlist
function displayList() {
let i;
for(i = head; i != null; i = i.next) {
console.log(i.value)
}
return;
}

// Insert the given value at the given position
function insertAtGivenPosition() {
let position = document.getElementById('insertInPosition').value;
let value = document.getElementById('insertValue2').value;
let tempNode = createNode(value);
let i,j;
for(i = head, j = 0 ; j < position; i = i.next, j++);
tempNode.next = i.next;
i.next = tempNode;
}

// Delete the node from the given position
function deleteFromPosition() {
let position = document.getElementById('deleteFromPosition').value;
let i,j;
for(i = head, j = 0 ; j < position; i = i.next, j++);
i.next = i.next.next;
}

// Print the middle value of the linkedlist
function printMiddleOfLinkedList() {
let i;
for(i = head, j = head; j.next != null && j.next.next != null; i = i.next, j = j.next.next);
console.log(i.value);
}

Comments