Create a Linked List Class Code help

Tell us what’s happening:

I kind of understood the code below. I did not fully understand from 7. to the rest. Can someone explain to me the code from top to bottom? I think this will help me to clarify my understanding. Thank you.

Your code so far

function LinkedList() {
var length = 0;
var head = null;

var Node = function(element){
  this.element = element; = null;

7. this.head = function(){
  return head;

this.size = function(){
  return length;

this.add = function(element){
  // Only change code below this line
var node = new Node(element);
if (head){
var current = head;
while(!== null){
  current =;
} = node;
  else {
    head = node;
  // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Create a Linked List Class

Link to the challenge:

Not sure anyone is going to take the time to explain everything when you can find plenty of tutorials about linked lists online:

If you have a specific question about something in the code maybe you might get more bites.

@bbsmooth thank you for the article recommendation. That article helped.

Do you understand what “this” means?

@bradhanks I think, in this code ‘this’ used for defining the head.
I am getting hard time to understand the code inside add function.

@Touhedul11 “this” refers to the class LinkedList.
The stuff inside the function using “this” is assigning properties methods to LinkedList.
You can see this by creating an instance of LinkedList.

let newLinkedList = new LinkedList();
//what is value of head?
//access value of head using class method

Try changing this.head = … to var head = …
What’s the output from the console log statement above?

//you can add a property manually
//add a variable
newLinkedList.newVar = "newVar value";
//we can also add a method to access that value.
newLinkedList.getNewVar = () => return newLinkedList.newVar;

If we created the property and method inside the class, we could just put do the following.

this.newVar = "newVar value";
this.getNewVar = () => return this.newVar;

Variables assigned to a class using “this” are referred to as the class’s properties and functions assigned to a class using “this” are referred to as a class’s methods.

I hope that helps you look at the problem with fresh eyes.

1 Like

Wow that’s eye opening . I will try to solve them and get the idea. I appreciate the way you explained. Later, i will share my thoughts if needed. Thank you again.