Intro
Last time, we learned how to pop a new node from the end of our Singly Linked List.
Today, we learn how to unshift something to the list.Unshift meansadd something to the beginning.
Current Code
We start with the codefrom the setup, withoutpush andpop, because we want to keep the code as simple as possible to understand.
classNode{constructor(value){this.value=value;this.next=null;}}classSinglyLinkedList{constructor(){this.length=0;this.head=null;this.tail=null;}}Thoughts
First, we should think about the constraints and possibilities:
If there is currently NO other node in the Singly Linked List (so it is currently empty):
- create a new node
- set the new node as the Singly Linked List's
tail - set the new node as the Singly Linked List's
head - increase the Singly Linked List's length by 1
- return the new node
If there is at least 1 node in the Singly Linked List:
- create a new node
- set the new node's
nextto the Singly Linked List's currenthead - set the new node as the Singly Linked List's
head - increase the Singly Linked List's length by 1
- return the new node
Examples:
- 0 nodes: before: null (head & tail) => after: A (head & tail)
- 1 node: before: A (head & tail) => after: A-1 (head) -> A (tail)
- n nodes: before: A (head) -> ... -> n (tail) => after: A-1 (head) -> A -> ... -> n (tail)
Differences:
- there is only one difference: the step after creating a new node
Implementation (Short version, DRY)
classNode{constructor(value){this.value=value;this.next=null;}}classSinglyLinkedList{constructor(){this.length=0;this.head=null;this.tail=null;}unshift(value){// create a new nodeconstnewNode=newNode(value);// check if Singly Linked List is emptyif(!this.length){// set the new node as the Singly Linked List's `tail`this.tail=newNode;}else{// set the new node's `next` to the Singly Linked List's current `head`newNode.next=this.head;}// set the new node as the Singly Linked List's `head`this.head=newNode;// increase the Singly Linked List's length by 1this.length+=1;// return the new nodereturnnewNode;}}Result
Let's have a look how to use the Singly Linked Listunshift method and its results.
constnewSLL=newSinglyLinkedList();// should be emptyconsole.log(newSLL);// SinglyLinkedList { length: 0, head: null, tail: null }console.log(newSLL.unshift("1"));// Node { value: '1', next: null }// should be a list with the new node with value 1console.log(newSLL);/* * SinglyLinkedList { * length: 1, * head: Node { value: '1', next: null }, * tail: Node { value: '1', next: null } * } */console.log(newSLL.unshift("2"));// Node { value: '2', next: Node { value: '1', next: null } }// should be a list with the new node with value 2 and 1 (from the last unshift)console.log(newSLL);/* * SinglyLinkedList { * length: 2, * head: Node { value: '2', next: Node { value: '1', next: null } }, * tail: Node { value: '1', next: null } * } */Next Part
We will implement how to remove a node from the beginning of the Singly Linked List. If you want to be notified,subscribe :)
Questions:
- Any ideas how to improve the post or code?
- Any specific questions?
- Do you like the series or is it useless? Why?
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse



