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

linkedlist method in js

/* amethod to find the maximum node value in the linkedlist*/ 
maxNode(list) {
            let current = list.head;
            let max = 0;
            while (current) {
                if (current.value > max) {
                    max = current.value
                }
                current = current.next;
            }
            return max;
        }
Comment

PREVIOUS NEXT
Code Example
Javascript :: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 
Javascript :: how to use paystack with react 
Javascript :: javascript advanced interview questions 
Javascript :: base64 to base64url 
Javascript :: addeventlistener 
Javascript :: queryinterface select 
Javascript :: run javascript runtime 
Javascript :: alert in react native 
Javascript :: difference between || and ?? in js 
Javascript :: javascript Program to check if a given year is leap year 
Javascript :: react rating 
Javascript :: jquery modal 
Javascript :: mergesort 
Javascript :: Getting One Value from an Array of Items 
Javascript :: js test library 
Javascript :: .then(async 
Javascript :: javascript var in quotes 
Javascript :: convert decimal to hex 
Javascript :: discord js slash command 
Javascript :: react animations 
Javascript :: javascript making a tag game 
Javascript :: rxjs of 
Javascript :: http error 406 
Javascript :: angular flex layout 
Javascript :: How do I use for-loops js 
Javascript :: javascript spread syntax 
Javascript :: js remove entry 
Javascript :: how to get last element in array java scipt 
Javascript :: .has js 
Javascript :: variables in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =