When adding an element at the end of a LinkedList object, there can be two scenarios: one where the list is empty and we are adding its first element, or one where the list is not empty and we are appending elements to it.
The following is the implementation of the push method:
push(element) { const node = new Node(element); // {1} let current; // {2} if (this.head == null) { // {3} this.head = node; } else { current = this.head; // {4} while (current.next != null) { // {5} get last item current = current.next; } // and assign next to new element to make the link current.next = node; // {6} } this.count++; // {7} }
The first thing we need to do is create a new Node passing element as its ...