Linked List Method Problem

Hi i’m following a tutorial a Linked List tutorial but i can’t access the method from the class LinkedList:
Here’s the tutorial: How to Implement a Linked List in JavaScript
Code:

// Listas atrelaçadas através de nós que se assemelham como arrays

// Criação da lista de nós a qual receberá os valores

class ListNode {

constructor(data) {

    this.data = data;

    this.next = null;   

}

}

// Modificador

class LinkedList {

constructor(head = null){

    this.head = head;

} 

size() {

    let count = 0;

    let node = this.head;

    while(node){

        count++;

        node = node.next

    }

    return count;

}

}

// Exemplos em prática

let node1 = new ListNode(2);

let node2 = new ListNode(50);

let node3 = new ListNode(6);

node1.next = node2;

node1.next = node3;

let list = new LinkedList(node1);

// Outros métodos

console.log(list.head.next.data) //returns 5

1 Like

I’m not sure if I understand correctly. Other than the constructor, there is only one method defined in your LinkedList. I am able to access it:

console.log(list.size()); → returns 2

If you were referring to something else then you’ll need to provide more details.

Also, when you paste code in here you need to wrap it in triple backticks. Put three backticks on a line by itself, then the code underneath, then three more backticks on a line by itself underneath the code.

Finally, you have three nodes in your list but size() returned 2 so I think there might be a bug there.

Thank you I think i understand the problem