Unable to understand this magic code

I completed my full stack certification. Now I am trying to solve data structure.
Actually I am trying to solve the linked list challenge. I understood how to write add method to add element to linked list. but I am not able to understand one thing.
This code I got from Beau from you tube.

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

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

  }

  this.add = function(value) {
    var node = new Node(value)

    if (head === null) {
      head = node

    }
    else {
      var currentNode = head

      console.log(head, 'head')
      head = currentNode

      while (currentNode.next) {

        currentNode = currentNode.next
      }

      currentNode.next = node;

    }
  }
}

var conga = new LinkedList();
conga.add(1);
conga.add(2);
conga.add(3);
conga.add(4);
conga.add(5);

This is that code.
Actually if you look at this code ,you can see that
first,he assigned value for

height=null

that is okay. I know why height is assigned as a value null.
But after that you see

if (head === null) {
  head=node
}

I understood this also.
I did not understand only one thing from else block
In else block you can see he assigned

currentNode = head

but he did not assign any value in else block for head
he only assigned value for head in if block. if block will only get executed once.
but if you do console.log(head) from if block ,you can see that the value of head is getting updated.
how is it possible that one variable value is getting changed without assigning any value?
if he assigned

head=currentNode

in else block, I would not ask this question.
Can you please help this issue?

What is the one thing you do not understand?

Unable to post because of it is asking ``` back ticks

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

I am not sure what video you are using, but the the line after your console.log statement shown below is not even needed.

      console.log(head, 'head')
      head = currentNode

The following would work just fine.

this.add = function(value) {
    var node = new Node(value)

    if (head === null) {
      head = node
    }
    else {
      var currentNode = head
      while (currentNode.next) {
        currentNode = currentNode.next
      }
      currentNode.next = node;
    }
  }

There is a video on youtube from Freecode camp on data structure and algorithm.
I will just post the link of code pen of linked list code below

https://codepen.io/beaucarnes/pen/ybOvBq?editors=0011

I have only doubt in else block
Now I am concentrating only on add method.
I did not understand only one part from else block.
in if block variable head is assigned with the value of node. but in if block there is a condition that if head is equal to null , then only if block will get executed. as the value of head at the first time, this if block will get executed once. after that the value of head is not null. so if you call the method after first time, this if block wont get executed.
you can see that we did not assign any value for the variable head in else block. we only assign the value in if block for head. but if you do
console.log(head)
from else block, you can see that each time you add the value to linked list , the value of head is also getting changed. that part I did not understand

I think if the value of head is to be changed, that if block should get executed. but that will only executed once. then how the value of head is getting changed each time we call add method

console.log I used there for understanding how the value of variable head is getting changed.
Hope you help me solving my doubt

What you are missing here is that the above assigns a reference to the head object, so any changes to currentNode affects head, because they reference the same object. That is why the head variable changes.

Thank you for replaying. But I did not understand properly
var currentNode = head
here we assign the value of head to curretNode. So that currentNode variable value will get updated each time invoking the method add.

Are you saying that the value of head variable getting changed from if block or else block. Please tell me. I am worried as I am not able to understand this part.;

There is a method
this.elementAt
which use for getting the value out from linked list. if you check that, you can see that linked list values is getting stored in head?
I only want to know where that assignment part going on so that the value of head is getting changed. Can you please post that code here.? Can you please tell me whether the value of head is getting changed from if block or else block.

If you are referring to the code that I posted as a reply under the comment “The following would work just fine.”, then I am saying the head gets updated everytime you make a change to currentNode.

When the linked list is empty and a value is added (i.e conga.add(1)), then head gets reassigned a new reference (node). If the linked list is not empty (i.e head is not equal to null), then you assign currentNode a reference to the head object with the following line:

var currentNode = head

At this point in the code, currentNode and head are the exact same object. That is why you see a change in head.

with all due respect let me say that I dont think this code
var currentNode = head
is the reason for getting the value of head is upadated.
To verify that I declare one more variable. confirmation which you can see in the code.
then in if block, I just assign
confirmation=node

and in else block I just do
console.log(confirmation) and found that the value of confirmation is also getting changed.(confirmation value and head value were found equal !)
So my final assumption is that , if the value of confirmation is getting changed, that happen from if block. but I don’t know know’
I am extremely sorry to ask you again and again. I don’t have any other forum expect freecoddecamp to ask my doubts.

  var head = null
var confirmation=null
  var length = 0

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

  }

  this.add = function(value) {
    var node = new Node(value)

    if (head === null) {
      head = node
confirmation=node
    }
    else {
      var currentNode = head
console.log(confirmation)
      console.log(head, 'head')
      head = currentNode

      while (currentNode.next) {

        currentNode = currentNode.next
      }

      currentNode.next = node;

    }
  }
}

var conga = new LinkedList();
conga.add(1)
conga.add(2)
conga.add(3)
conga.add(4)
conga.add(5);```

Now you are talking about your original code again. I was referring to the code I posted. I don’t know how else to tell you, but the following line is not needed.

head = currentNode

In the first call of conga.add(1), head is null initially, because the linked list was created with head = null. Because head is null, the if statement evaluates to true and you assign head a reference to the node object.

In the next call of conga.add(2), head is no longer null, so the else statement code block executes. Referring to the code I posted, the var currentNode is assigned a reference to the head object. At this point head has not changed. The while loop condition evalutes to false, because you are at the first value (really just the head), so the next property is null. After the while loop code, you assign curretntNode.next a reference to node (the new object containing 2 as the value of its element property). At this point head has changed, because it now contains an element property of value 1 and a next property containing an object with an element property value of 2 and a next property value of null.

This continues on each time.

If you add a head method and some console.log statements, you can see what is actually happening to the value of head.

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

  this.add = function(value) {
    var node = new Node(value)

    if (head === null) {
      head = node
    }
    else {
      var currentNode = head
      while (currentNode.next) {
        currentNode = currentNode.next
      }
      currentNode.next = node;
    }
    console.log('head');
    console.log(JSON.stringify(head, null, '  '));
    console.log('***************');
  }

Running your example code, you would see something like the following in the console:

head
{
  "element": 1,
  "next": null
}
***************
head
{
  "element": 1,
  "next": {
    "element": 2,
    "next": null
  }
}
***************
head
{
  "element": 1,
  "next": {
    "element": 2,
    "next": {
      "element": 3,
      "next": null
    }
  }
}
***************
head
{
  "element": 1,
  "next": {
    "element": 2,
    "next": {
      "element": 3,
      "next": {
        "element": 4,
        "next": null
      }
    }
  }
}
***************
head
{
  "element": 1,
  "next": {
    "element": 2,
    "next": {
      "element": 3,
      "next": {
        "element": 4,
        "next": {
          "element": 5,
          "next": null
        }
      }
    }
  }
}
***************
1 Like

Thanks for helping me.
where should we use linked list? Because in JavaScript we can store how many
element we want to push to array, we can do that dynamically. in java As we can not change the length of array, linked list is an option to store data dynamically?
So Can you please tell me this linked list , Binary search tree, avl tree is requred in JavaScript also? can you tell me the application of these types of data structure?