JavaScript Exercises: Remove a node at the specified index in a singly linked list
JavaScript Singly Linked List: Exercise-11 with Solution
Write a JavaScript program that removes the node from the singly linked list at the specified index.
Sample Solution:
JavaScript Code:
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class SinglyLinkedList {
constructor(Head = null) {
this.Head = Head
}
add(newNode){
let node = this.Head;
if(node==null){
this.Head = newNode;
return;
}
while (node.next) {
node = node.next;
}
node.next = newNode;
}
removeAt(index_pos){
let nodes = this.Head;
if(index_pos===0) {
if(nodes!==null)
{
nodes = nodes.next;
this.Head = nodes;
}
else
throw Error("Index Out of Bound");
return;
}
while(--index_pos){
if(nodes.next!==null)
nodes = nodes.next;
else
throw Error("Index Out of Bound");
}
nodes.next = nodes.next.next;
}
displayList(){
let node = this.Head;
var str = ""
while (node) {
str += node.data + "->";
node = node.next;
}
str += "NULL"
console.log(str);
}
}
let numList = new SinglyLinkedList();
numList.add(new Node(12));
numList.add(new Node(13));
numList.add(new Node(14));
numList.add(new Node(15));
numList.add(new Node(14));
numList.displayList();
console.log("Remove the node where index position is 0:")
numList.removeAt(0)
numList.displayList();
console.log("Remove the node where index position is 3:")
numList.removeAt(3)
numList.displayList();
console.log("Remove the node where index position is 2:")
numList.removeAt(2)
numList.displayList();
Sample Output:
12->13->14->15->14->NULL Remove the node where index position is 0: 13->14->15->14->NULL Remove the node where index position is 3: 13->14->15->NULL Remove the node where index position is 2: 13->14->NULL
Flowchart:

Live Demo:
See the Pen javascript-singly-linked-list-exercise-11 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Singly Linked List Previous: Clear a singly linked list by pointing the head towards null.
Singly Linked List Next: Size of a Singly Linked list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
defineProperty method
const person = { name: 'Owen' }; Object.defineProperty(person, 'age', { value: 21 }); console.log(person); console.log(Object.keys(person));
With the defineProperty method, we can add new properties to an object, or modify existing ones. When we add a property to an object using the defineProperty method, they are by default not enumerable. The Object.keys method returns all enumerable property names from an object, in this case only "name".
Properties added using the defineProperty method are immutable by default. You can override this behavior using the writable, configurable and enumerable properties. This way, the defineProperty method gives you a lot more control over the properties you're adding to an object.
Ref: https://bit.ly/323Y0P6
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook