- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The Linked List Class in Javascript
Here is the complete implementation of the LinkedList class −
Example
class LinkedList { constructor() { this.head = null; this.length = 0; } insert(data, position = this.length) { let node = new this.Node(data); if (this.head === null) { this.head = node; this.length++; return this.head; } let iter = 1; let currNode = this.head; while (currNode.next != null && iter < position) { currNode = currNode.next; iter++; } node.next = currNode.next; currNode.next = node; this.length++; return node; } remove(data, position = 0) { if (this.length === 0) { console.log("List is already empty"); return; } this.length--; let currNode = this.head; if (position <= 0) { this.head = this.head.next; } else if (position >= this.length - 1) { while (currNode.next.next != null) { currNode = currNode.next; } currNode.next = null; } else { let iter = 0; while (iter < position) { currNode = currNode.next; iter++; } currNode.next = currNode.next.next; } return currNode; } display() { let currNode = this.head; while (currNode != null) { console.log(currNode.data + " -> "); currNode = currNode.next; } } } LinkedList.prototype.Node = class { constructor(data) { this.data = data; this.next = null; } };
- Related Articles
- The Doubly Linked List class in Javascript
- Linked List representation in Javascript
- Linked List Data Structure in Javascript
- Types of Linked List in Javascript
- Singly Linked List as Circular in Javascript
- Doubly Linked List as Circular in Javascript
- Creating a linked list using Javascript
- Remove elements from singly linked list in JavaScript
- How to Delete a Linked List in JavaScript?
- Creating a Doubly Linked List using Javascript
- JavaScript Program for Finding the Length of Loop in Linked List
- Finding middlemost node of a linked list in JavaScript
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Add elements to a linked list using Javascript

Advertisements