Linked List in JavaScript

Linked lists are a linear data structure that consists of a sequence of nodes. Each node contains a value and a pointer to the next node in the list. Linked lists are dynamic, which means that they can grow and shrink as needed.

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.length = 0;
  }

  add(value) {
    const newNode = new Node(value);
    if (this.head === null) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next !== null) {
        current = current.next;
      }
      current.next = newNode;
    }
    this.length++;
  }

  print() {
    let current = this.head;
    while (current !== null) {
      console.log(current.value);
      current = current.next;
    }
  }
}

const linkedList = new LinkedList();

linkedList.add(1);
linkedList.add(2);
linkedList.add(3);

linkedList.print(); // 1 2 3

Read More Topics

System Design Syllabus

Introduction to System Design

Architectural Patterns in System Design

Scalability and Performance in System Design

Database Design in System Design

Distributed Systems in System Design

System Integration and APIs in System Design

Cloud Computing in Sestem Design

Containerization and Orchestration in System Design

High Availability and Disaster Recovery

Security in System Design

Performance Tuning and Optimization

Case Studies and Real-World Projects

B.Tech 4 YEAR CS/IT PROJECTS And Placement

Face Detection Project Idea

Weather Forecasting APP in MERN Project

JavaScript Tips and Trick

Arrays in data Structure

All DSA Topics in JavaScript

e-commerce website React.js logic code

JavaScript function that could be used to add a product to the shopping cart

Leave a Comment

Skip to content