- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing Elements from a Double Linked List using Javascript
Removing an element is very easy in a linked list. We just need to get rid of the node we want to remove, ie, lose its reference. There are 3 cases we need to consider −
- Removing an element from head: In this case, we can simply assign head = head.next and remove the previous link from the next element. This way we'll lose the reference of the first element. And out head will start pointing to the second element.
- Removing an element from the tail: In this case, we can simply assign the node.next of second last node to be null and we'll get rid of the last element from the list. We also update the tail to point to a current node.
- Removing an element from in between: This is more tricky. In this case, we'll have to make the node before the node we want to remove, to directly point to the node after the node we want to remove. So, prevNode.next = node.next and node.next.prev = prevNode will do this for us.
Now let's see an illustration of this −
Now let's have a look at how we'll implement this −
Example
remove(data, position = 0) { if (this.length === 0) { console.log("List is already empty"); return; } this.length--; let currNode = this.head; if (position <= 0) { this.head = this.head.next; this.head.prev = null; } else if (position >= this.length - 1) { this.tail = this.tail.prev; this.tail.next = null; } else { let iter = 0; while (iter < position) { currNode = currNode.next; iter++; } currNode.next = currNode.next.next; currNode.next.prev = currNode; } return currNode; }
Example
You can test this using −
let list = new LinkedList(); list.insert(10); list.insert(20); list.insert(30); list.remove(1); list.display(); list.insert(15, 2); list.remove(); list.display();
Output
This will give the output −
20 <-> 30 <-> 30 <-> 15 <->
- Related Articles
- Remove elements from a linked list using Javascript
- Add elements to a linked list using Javascript
- Inserting Elements to a doubly linked list using Javascript
- Remove elements from singly linked list in JavaScript
- Creating a linked list using Javascript
- Removing redundant elements from array altogether - JavaScript
- Creating a Doubly Linked List using Javascript
- Removing punctuations from a string using JavaScript
- Removing all the elements from the List in C#
- C program to insert a node at any position using double linked list
- Removing all spaces from a string using JavaScript
- Python program to remove duplicate elements from a Circular Linked List
- Remove Duplicates from a Sorted Linked List Using Recursion
- Python program to remove duplicate elements from a Doubly Linked List\n
- Explain insertion of elements in linked list using C language

Advertisements