
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Remove elements from singly linked list in JavaScript
Suppose, we have a singly linked list like this −
const list = { value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: { value: 5, next: { value: 6, next: { value: 7, next: null } } } } } } };
We are required to write a JavaScript function that takes in one such list as the first argument and a number as the second argument.
The function should search whether there exists a node with that value in the list, if it does, the function should remove the node from the list.
Example
The code for this will be −
const list = { value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: { value: 5, next: { value: 6, next: { value: 7, next: null } } } } } } }; const recursiveTransform = (list = {}) => { if(list && list['next']){ list['value'] = list['next']['value']; list['next'] = list['next']['next']; return recursiveTransform(list['next']); }else{ return true; }; } const removeNode = (list = {}, val, curr = list) => { // end reached and item not found if(!list){ return false; } if(list['value'] !== val){ return removeNode(list['next'], val, list); }; return recursiveTransform(list); }; console.log(removeNode(list, 3)); console.log(JSON.stringify(list, undefined, 4));
Output
And the output in the console will be −
true { "value": 1, "next": { "value": 2, "next": { "value": 4, "next": { "value": 6, "next": { "value": 7, "next": null } } } } }
- Related Articles
- Remove elements from a linked list using Javascript
- Singly Linked List as Circular in Javascript
- Golang program to remove elements from the linked list
- Find smallest and largest elements in singly linked list in C++
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Find minimum and maximum elements in singly Circular Linked List in C++
- Difference between Singly linked list and Doubly linked list in Java
- Python program to remove duplicate elements from a Circular Linked List
- Delete all Prime Nodes from a Singly Linked List in C++
- Removing Elements from a Double Linked List using Javascript
- Python program to remove duplicate elements from a Doubly Linked List\n
- Delete all Non-Prime Nodes from a Singly Linked List in C++
- Binary Search on Singly Linked List in C++
- C++ Program to Implement Singly Linked List

Advertisements