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

linkedlist javascript

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

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

let node1 = new ListNode(2);
let node2 = new ListNode(4);
node1.next = node2;

let list = new LinkedList(node1);

console.log(list.head.next);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript trigger change event 
Javascript :: javascript read file lines into array vanilla 
Javascript :: compare 2 array element 
Javascript :: Warning: Failed child context type: Invalid child context `virtualizedCell.cellKey` of type 
Javascript :: js math.random 
Javascript :: js obj getting count of properties 
Javascript :: How to get input file using js 
Javascript :: can you call api in next.js getserverside props 
Javascript :: js enum 
Javascript :: laravel data return in json 
Javascript :: js loop an array 
Javascript :: javascript last character 
Javascript :: how to add toaster in angular 9 
Javascript :: basic server on node.js 
Javascript :: javascript how-do-i-copy-to-the-clipboard-in-javascript 
Javascript :: javascript detect if object is date 
Javascript :: how to install yup in react native 
Javascript :: open new window chrome extension 
Javascript :: useref 
Javascript :: safeareaview react native android 
Javascript :: javascript array to string 
Javascript :: How to hthe amount of users online in discordjs 
Javascript :: set label text in jquery 
Javascript :: how to read json file in python stack overflow 
Javascript :: dynamic folder import javascript 
Javascript :: react transition group 
Javascript :: how to hide a input and label jquery 
Javascript :: change span value javascript 
Javascript :: jquery add table row 
Javascript :: javascript replace hyphen with space 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =