Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

linked list javascript

// Linked Lists of nodes
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  // Add a value at beginning of list
  addStart(value) {
    const node = new Node(value);
    node.next = this.head;
    this.head = node;
  }

  // Add a value at end of list
  addEnd(value) {
    const node = new Node(value);
    let curr = this.head;
    if (curr == null) {
      this.head = node;
      return;
    }

    while (curr !== null && curr.next !== null) {
      curr = curr.next;
    }

    curr.next = node;
  }
}

const list = new LinkedList();
list.addStart(1);
list.addStart(2);
list.addEnd(3);

console.log(list.head.value); // 2 (head of list)
Comment

linked list algorithm javascript

// Linked Lists of nodes
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  // Add a value at beginning of list
  addStart(value) {
    const node = new Node(value);
    node.next = this.head;
    this.head = node;
  }

  // Add a value at end of list
  addEnd(value) {
    const node = new Node(value);
    let curr = this.head;
    if (curr == null) {
      this.head = node;
      return;
    }

    while (curr !== null && curr.next !== null) {
      curr = curr.next;
    }

    curr.next = node;
  }

  // return the size of the link list
  size() {
    let count = 0;
    const node = this.head;
    if (!node) return count;
    while (node) {
      count++;
      node = node.next;
    }
    return count;
  }

  // empty the node
  clear() {
    this.head = null;
  }

  getFirst() {
    return this.head;
  }

  getLast() {
    let lastNode = this.head;
    if (lastNode) {
      while (lastNode.lastNode) {
        lastNode = lastNode.next;
      }
    }
    return lastNode;
  }
}

const list = new LinkedList();
list.addStart(1);
list.addStart(2);
list.addEnd(3);

console.log(list.head.value); // 2 (head of list)
Comment

PREVIOUS NEXT
Code Example
Javascript :: send request express 
Javascript :: number format reactjs 
Javascript :: get date format javascript 
Javascript :: object javascript 
Javascript :: how to identify index of nested arrays in javascriptn 
Javascript :: noty js 
Javascript :: react native image viewer 
Javascript :: if array javascript 
Javascript :: bottom navigation bar react native hide on keyboard 
Javascript :: javascript loop aray 
Javascript :: cypress check if an element is visible 
Javascript :: json object in html page 
Javascript :: javascript append array to end of array 
Javascript :: javascript document 
Javascript :: take one character in end of string javascript 
Javascript :: jquery basics 
Javascript :: javascript if return true false 
Javascript :: json_extract in non native query 
Javascript :: read and save excel with react 
Javascript :: javascript json 
Javascript :: angular import service 
Javascript :: toggle buttons angular styles 
Javascript :: quote 
Javascript :: create object filter 
Javascript :: serviceworker in angular 
Javascript :: average javascript 
Javascript :: for of 
Javascript :: make indexOF in js 
Javascript :: vue on page link or anchor 
Javascript :: Array of indexOf 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =