data structures - LinkedList push() method in JavaScript with a tail - Stack Overflow

I try to understand how push() method works with the use of tails in JS. here is the code:class Node {

I try to understand how push() method works with the use of tails in JS. here is the code:

class Node {
    constructor(val) {
      this.val = val;
      this.next = null;
    }
  }
  class SinglyLinkedList {
    constructor() {
      this.length = 0;
      this.head = null;
      this.tail = null;
    }
    push(val) {
      const newNode = new Node(val)
      if (this.head===null) { // happens only once
        this.head = newNode;
        this.tail = this.head;
      } else {
        this.tail.next = newNode;   // this.a.next = b node???
        this.tail = newNode;
      }
      this.length++
    }

specifically, I don't understand the else part inside the push() method. How does each next of a head gets assigned a new node if we say this.tail.next = newNode? Where is the relation between head and tail and how by saying this.tail.next = newNode we get access to the head property of the list? When I run this code, it works perfectly correct and it confuses me.

const myList = new SinglyLinkedList();
  myList.push("111");
  myList.push("222");
  myList.push("333");
  console.log(myList);

Output:

SinglyLinkedList {
  length: 3,
  head: Node { val: '111', next: Node { val: '222', next: [Node] } },
  tail: Node { val: '333', next: null } }

I try to understand how push() method works with the use of tails in JS. here is the code:

class Node {
    constructor(val) {
      this.val = val;
      this.next = null;
    }
  }
  class SinglyLinkedList {
    constructor() {
      this.length = 0;
      this.head = null;
      this.tail = null;
    }
    push(val) {
      const newNode = new Node(val)
      if (this.head===null) { // happens only once
        this.head = newNode;
        this.tail = this.head;
      } else {
        this.tail.next = newNode;   // this.a.next = b node???
        this.tail = newNode;
      }
      this.length++
    }

specifically, I don't understand the else part inside the push() method. How does each next of a head gets assigned a new node if we say this.tail.next = newNode? Where is the relation between head and tail and how by saying this.tail.next = newNode we get access to the head property of the list? When I run this code, it works perfectly correct and it confuses me.

const myList = new SinglyLinkedList();
  myList.push("111");
  myList.push("222");
  myList.push("333");
  console.log(myList);

Output:

SinglyLinkedList {
  length: 3,
  head: Node { val: '111', next: Node { val: '222', next: [Node] } },
  tail: Node { val: '333', next: null } }
Share Improve this question asked Oct 22, 2020 at 6:05 ElchinElchin 1056 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

How does each next of a head gets assigned a new node if we say this.tail.next = newNode? Where is the relation between head and tail and how by saying this.tail.next = newNode we get access to the head property of the list?

Lets go back to the empty list. The first time a node is added, we get into the if block, where both head and tail will reference the same new node. This means that from that moment on, whatever you mutate in tail will be mutating head, since they refer to the same object.

Now a second push is executed and we get in the else block. There we assign the new node to the next property of tail. But since this is the same object that head refers to, we actually set head.next here! This only happens with the second push, because right after this assignment, tail is assigned a new reference (next) so from then on head and tail refer to different nodes.

Graphically:

After push('111'):

head
 ↓
111
 ↑
tail

When push('222'), after this.tail.next = newNode;:

head
 ↓
111 → 222
 ↑
tail

...and after this.tail = newNode; during that same push:

head
 ↓
111 → 222
       ↑
     tail

When push('333'), after this.tail.next = newNode;:

head
 ↓
111 → 222 → 333
       ↑
     tail

...and after this.tail = newNode; during that same push:

head
 ↓
111 → 222 → 333
             ↑
           tail

Okay I'll give it a try in explaining why that is.

  1. When you push the first time, both head and tail refers to the same node.
  2. Then when you push the second time, this.tail.next = newNode adds the new node to both head and tail's next property.
  3. Then this.tail = newNode updates the tail's node which leaves the head's next to be the same as tail.

If you want to check step 2 above, ment out this.tail = newNode and push twice. You will see both head and tail's next property is same.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180193a4615383.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信