Last time, we setup our Singly Linked List.
Today, we learn how to push something to the list. Push
means add something to the end
.
Node
Singly Linked List
Node
classSingly Linked List
class// name of the class
class Node {
// the constructor runs when using the class with `new` (see later)
constructor(value) {
// set this nodes value property to the instantiation value
this.value = value;
// set this nodes next property to `null`
this.next = null;
}
}
// name of the class
class SinglyLinkedList {
// the constructor runs when using the class with `new`
constructor() {
// set this lists length property to `0`
this.length = 0;
// set this lists head property to `null`
this.head = null;
// set this lists tail property to `null`
this.tail = null;
}
}
First, we should think about the constraints and possibilities:
If there is already at least one other node in the Singly Linked List:
next
property to the new node (so the new node comes after the current tail)tail
to the new nodeIf there is currently NO other node in the Singly Linked List (so it is currently empty):
head
to the new nodetail
to the new nodeThe differences?
Example:
So A
always stays the head, only if we got 0 nodes, we have to set A
as the new head
.
In every other situation, we have to point the current tail
to the new node and make the new node the new tail
.
Thoughts
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
}
push(value) {
/*
* 1. If there is already at least one other node in the Singly Linked List
*/
if (this.length > 0) {
// create a new node with an input value
const newNode = new Node(value);
// point the current tails `next` property to the new node
this.tail.next = newNode;
// set the Singly Linked List's `tail` to the new node
this.tail = newNode;
// increase the Singly Linked List's length by 1
this.length += 1;
// return the new node
return newNode;
} else {
/*
* 2. If there is currently NO other node in the Singly Linked List (so it is currently empty):
*/
// create a new node with an input value
const newNode = new Node(value);
// set the Singly Linked List's `head` to the new node
this.head = newNode;
// set the Singly Linked List's `tail` to the new node
this.tail = newNode;
// increase the Singly Linked List's length by 1
this.length += 1;
// return the new node (so that we knew what we added)
return newNode;
}
}
}
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
}
push(value) {
const newNode = new Node(value);
if (this.length > 0) {
this.tail.next = newNode;
} else {
this.head = newNode;
}
this.tail = newNode;
this.length += 1;
return newNode;
}
}
Let’s have a look how to use the Singly Linked List push
method and its results.
const newSLL = new SinglyLinkedList();
console.log(newSLL);
/*
* SinglyLinkedList {
* length: 0,
* head: null,
* tail: null
* }
*/
newSLL.push("A");
console.log(newSLL);
/*
* SinglyLinkedList {
* length: 1,
* head: Node { value: 'A', next: null },
* tail: Node { value: 'A', next: null }
* }
*/
newSLL.push("B");
console.log(newSLL);
/*
* SinglyLinkedList {
* length: 2,
* head: Node { value: 'A', next: Node { value: 'B', next: null } },
* tail: Node { value: 'B', next: null }
* }
*/
We will implement how to remove a node from the end of the Singly Linked List. If you want to be notified, subscribe :)
Hi! I'm Michael 👋 I'm a Mentor & Senior Web Developer - I help you to reach your (career) goals.