skip to Main Content
class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

class SLL {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  push(val) {
    let newNode = new Node(val)
    if (!this.head) {
      this.head = newNode;
      this.tail = this.head;
    } else {
      console.log(this.tail);
      console.log(this.tail.next);
      debugger
      this.tail.next = newNode;
      this.tail = newNode;
    }
    this.length++;
  }
}

let x = new SLL();
x.push('Hi');
x.push('Hello');
x.push('How');
x.push('Are');
x.push('You');
x.push('Man');

Doubt:
After the 2nd push(‘Hello’), the tail is assigned with the newNode "this.tail = newNode" under else part. But in the 3rd push "push(‘How’)" how the this.tail is still referencing the this.head.

I have done console.log of both "this.tail" and "this.tail.next".The output shows fresh node. Am not able to understand how this still reference to the this.head.

enter image description here

enter image description here

Thanks for the answers!

2

Answers


  1. class Node {
      constructor() {
        this.value = null;
        this.next = null;
      }
    }
    
    class SinglyLinkedList {
      constructor() {
        this.root = null;
      }
    
      insert(value) {
        // create object of node class
        let newNode = new Node();
    
        if (!this.root) {
          newNode.value = value;
          this.root = newNode;
          return;
        }
    
        let current = this.root;
    
        // traversing to get the node whose next node pointer is null
        // this is to get one previous node to get access of next node whoose next pointer address is null
        while (current.next != null) {
          current = current.next;
        }
        //assign value to newNode
        newNode.value = value;
        //attach newNode to linked list
        current.next = newNode;
      }
    
      //   To print all nodes
      printAllNodes() {
        let current = this.root;
        while (current != null) {
          console.log(current.value);
          current = current.next;
        }
      }
    }
    
    let list = new SinglyLinkedList();
    
    list.insert(5);
    list.insert(10);
    list.insert(15);
    list.insert(20);
    
    list.printAllNodes();
    
    // console.log(list.root);
    Login or Signup to reply.
  2. Linked list implementation with Tail address pointer

    class Node {
      constructor() {
        this.value = null;
        this.next = null;
      }
    }
    
    class SinglyLinkedList {
      constructor() {
        this.root = null;
        this.tail;
      }
    
      insert(value) {
        // create object of node class
        let newNode = new Node();
    
        if (!this.root) {
          newNode.value = value;
          this.root = newNode;
          this.tail = this.root;
          return;
        }
    
        let current = this.tail;
        //assign value to newNode
        newNode.value = value;
    
        current.next = newNode;
        //update the tail to newly inserted Node address
        this.tail = current.next;
      }
    
      //   To print all nodes
      printAllNodes() {
        let current = this.root;
        while (current != null) {
          console.log(current.value);
          current = current.next;
        }
      }
    }
    
    let list = new SinglyLinkedList();
    
    list.insert(5);
    list.insert(10);
    list.insert(15);
    list.insert(20);
    list.insert(25);
    
    list.printAllNodes();
    
    // console.log(list.root);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search