Java Script linked list

I just read that blog about linked list and tried to do that then I got an output but that output is not match with the blog author. I need some help

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

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

// putting all together
let node1 = new ListNode(2);
let node2 = new ListNode(5);
// console.log(node1);
node1.next = node2;

// let's create a linklist with node1
let list = new LinkList(node1);

console.log(list.head.data); //output-: 2 but expected 5

What’s the difference? A contrast result? Depends on preference, it can be done the other way too:

node2.next = node1;
let list = new LinkList(node2)

What I meant by preference is, for adding a node, User can add to the head, or add to the tail. Each has its pros and cons, will depends a lot on the situation and what kind of data being stored in the linklist.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.