How the object "this.head" is updated without assignement?
class Node { constructor(val) { this.val = val; this.next = null; } } class SinglyLinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } push(val) { var newNode = new Node(val); if (!this.head) { this.head = newNode; this.tail = this.head; } else { this.tail.next = newNode; this.tail = newNode; } this.length++; return this; } pop() { if (!this.head) return undefined; var current = this.head; var newTail = current; while (current.next) { newTail = current; current = current.next; } this.tail = newTail; this.tail.next = null; this.length--; if (this.length === 0) { this.head = null; this.tail = null; } return current; } shift() { if (!this.head) return undefined; var currentHead = this.head; this.head = currentHead.next; this.length--; if (this.length === 0) { this.tail = null; } return currentHead; } unshift(val) { var newNode = new Node(val); if (!this.head) { this.head = newNode; this.tail = this.head; } newNode.next = this.head; this.head = newNode; this.length++; return this; } get(index) { if (index < 0 || index >= this.length) return null; var counter = 0; var current = this.head; while (counter !== index) { current = current.next; counter++; } return current; } set(index, val) { let foundNode = this.get(index); if (foundNode) { foundNode.val = val; } } } var list = new SinglyLinkedList(); list.push('1') list.push('2') list.push('3') list.set(0,'x')
Posted by youssefhoumazen 2020-11-14 05:40:10
No Ans Posted
Recent Links
- Random Clustering model -CppBuzz-Forum..
- C_Size-CppBuzz-Forum..
- value =-CppBuzz-Forum..
- reverse Function in C prog-CppBuzz-Forum..
- Mingw download with no installer-CppBuzz..
- Python Online Training-CppBuzz-Forum..
- function compare_to_interval-CppBuzz-For..
- rectangle.c -CppBuzz-Forum..
- Converting for loop to while loop-CppBuz..
- How to remove the (*) as we reach the en..
- more..