Javascript Singly Linked List And Its Methods
![, how to reverse a singly linked list in javascript.](https://todaybusinessposts.com/wp-content/uploads/2023/06/Screenshot-52.png)
A singly linked list in javascript is a data structure where elements are stored in separate nodes and each node contains a value and a reference to the next node in the list. A singly linked list is a type of linked list where each node only has a reference to the next node and does not have a reference to the previous node.
It is important to have a good grasp of singly linked lists because in many job interviews, online coding competitions, some questions gets asked like how to search in a singly linked list, how to reverse a singly linked list in javascript.
.
Node Class
Before creating a singly linked list in javascript, we need to understand the structure of a node as a linked list consists of nodes. Each node have a value and a next property that points to the next node in the list. To represent a node, we define a class with these two properties in javascript
class Node
constructor(value)
this.value = value;
this.next = null;
Singly Linked List
Now, we can create a singly linked list utilizing LinkedList class that will manage it. In this class, we Wii have the head property to access other nodes in the list. Firstly we will set it null as the list is empty.
class LinkedList
constructor()
this.head = null;
Adding Nodes
To add nodes to the linked list, we can define an add method in the LinkedList class. This method takes a value as a parameter and creates a new node with that value. If the list is empty, the new node becomes the head. Otherwise, we iterate through the list until we reach the last node and update its next property to point to the new node.
add(value)
const newNode = new Node(value);
if (!this.head) {this.head = newNode;
else {let current = this.head;
while (current.next) {current = current.next;}
current.next = newNode;}
Traversing Linked List
Traversing a linked list is starting from the head node and following the next pointers until the last node of the list is reached. We can create a traverse method inside the LinkedList class to print each node’s data.
traverse() {let current = this.head;
while (current) {console.log(current.data);current = current.next;}
Deleting Nodes
To delete a node from the linked list, we can define a delete method in the LinkedList class. This method takes a value as a parameter and removes the node with that value from the list. We need to consider different scenarios, such as deleting the head node, deleting an middle node or deleting the last node.
delete(value) {if (!this.head) {return;}
if (this.head.value === value) {this.head = this.head.next;return;}
let current = this.head;
while (current.next) {if (current.next.value === value) {current.next = current.next.next;return;}current = current.next;}
Example Usage
Let’s see an example of creating a singly linked list and performing some operations on it.
// Create a new linked list
const myList = new LinkedList();
// Add nodes to the list
myList.add(10);
myList.add(20);
myList.add(30);
myList.delete(30);
myList.traverse();
// 10 20
Conclusion
In JavaScript, a singly linked list is an important data structure. The Node class represents each node in the list, and the LinkedList class manages the list by providing methods to add and remove nodes and traverse the list to print each data in nodes.
Thank you for reading.