Delete Without Head Pointer using JavaScript
Last Updated :
22 Jul, 2024
The objective is to remove a node in a JavaScript singly linked list without having a direct reference to the head node. This entails making changes to the node that will be removed without actually using the head pointer.
Below are the approaches to deleting a node without a head pointer using JavaScript:
Copy Data and Adjust Pointers
This approach involves moving the data from the next node to the node that is to be deleted, changing pointers to skip the next node, and then deleting the next node.
- Copy Data from Next Node: Copy the data from the next node to the node to be deleted.
- Adjust Pointers: Set the next pointer of the node to be deleted to skip the next node.
- Delete Next Node: Delete the next node.
Example: To demonstrate deleting a node without a head pointer using JavaScript.
JavaScript
function deleteNode(node) {
if (node === null || node.next === null) {
return false;
// Cannot delete the last node or null node
}
node.data = node.next.data;
// Copy the data of the next node
node.next = node.next.next;
// Adjust the next pointer
return true;
}
// Node definition for the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to print the linked list
function printLinkedList(head) {
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
}
// Create a sample linked list
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
console.log("Original Linked List:");
printLinkedList(head);
// Deleting the node with data
// 2 (assuming we have reference to this node)
const nodeToDelete = head.next;
deleteNode(nodeToDelete);
console.log("\nAfter Deleting Node with Data 2:");
printLinkedList(head);
OutputOriginal Linked List:
1
2
3
4
After Deleting Node with Data 2:
1
3
4
Time Complexity: O(1)
Space Complexity: O(1)
Swap Data and Delete Next Node
This approach involves swapping the data of the node that has to be deleted with the data of the next node, after which the next node is deleted.
- Swap Data: Swap the data of the node to be deleted with the data of the next node.
- Delete Next Node: Delete the next node
Example: To demonstrate deleting a node without a head pointer using JavaScript.
JavaScript
function deleteNodeSwapData(node) {
if (node === null || node.next === null) {
return false;
// Cannot delete the last node or null node
}
let temp = node.data;
node.data = node.next.data;
node.next.data = temp;
node.next = node.next.next;
// Delete the next node
return true;
}
// Node definition for the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to print the linked list
function printLinkedList(head) {
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
}
// Create a sample linked list
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
console.log("Original Linked List:");
printLinkedList(head);
// Deleting the node with data 2
// (assuming we have reference to this node)
const nodeToDelete = head.next;
deleteNodeSwapData(nodeToDelete);
console.log("\nAfter Deleting Node with Data 2:");
printLinkedList(head);
OutputOriginal Linked List:
1
2
3
4
After Deleting Node with Data 2:
1
3
4
Time Complexity: O(1)
Space Complexity: O(1)
Traverse Entire List
Another approach, however inefficient, is to search through the whole list until you locate the node that has to be removed before any others. Once located, modify the pointers to eliminate the required node.
- Traverse List: Traverse the list to find the node before the node to be deleted.
- Adjust Pointers: Adjust pointers to skip the node to be deleted.
Example: To demonstrate deleting a node without a head pointer using JavaScript.
JavaScript
function deleteNodeTraverse(head, node) {
if (head === null || node === null) {
return false;
}
// Special case:
// If the node to be deleted is the head
if (head === node) {
head = head.next;
return true;
}
let current = head;
while ( current !== null
&&
current.next !== node
)
{
current = current.next;
}
if (
current === null
||
current.next === null
)
{
return false;
// Node not found
}
current.next = current
.next
.next;
// Adjust pointers to delete the node
return true;
}
// Define the Node class
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Create the linked list: 1 -> 2 -> 3 -> 4 -> 5
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
let nodeToDelete = head.next.next;
// Node with data 3
console.log("Before deletion:");
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
deleteNodeTraverse(head, nodeToDelete);
console.log("After deletion:");
current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
OutputBefore deletion:
1
2
3
4
5
After deletion:
1
2
4
5
Time Complexity: O(n) (worst case)
Space Complexity: O(1)
Method 4: Modify Next Node Reference
This approach involves directly modifying the reference of the node to be deleted by copying the reference of the next node and updating its data and next pointers accordingly.
- Copy Data and Reference: Copy the data and reference of the next node to the node to be deleted.
- Update Reference: Update the reference to point to the node after the next node.
Example:
JavaScript
function deleteNodeByReference(node) {
if (node === null || node.next === null) {
return false; // Cannot delete the last node or null node
}
node.data = node.next.data;
node.next = node.next.next;
return true;
}
// Node definition for the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to print the linked list
function printLinkedList(head) {
let current = head;
while (current !== null) {
console.log(current.data);
current = current.next;
}
}
// Create a sample linked list
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
console.log("Original Linked List:");
printLinkedList(head);
// Deleting the node with data 2 (assuming we have reference to this node)
const nodeToDelete = head.next;
deleteNodeByReference(nodeToDelete);
console.log("\nAfter Deleting Node with Data 2:");
printLinkedList(head);
OutputOriginal Linked List:
1
2
3
4
After Deleting Node with Data 2:
1
3
4
Similar Reads
Delete Operations in Doubly Linked List using JavaScript
This article will demonstrate how to perform delete operations in a Doubly Linked List Data Structure using JavaScript. We will see different approaches to deleting elements from the Doubly Linked List. Steps to Delete Element Node from Doubly Linked ListA doubly linked list has two pointers that li
11 min read
JavaScript Program to Delete Middle of Doubly Linked List
We are given a doubly linked list and we have to write a JavaScript program to delete the middle node of the list and print the newly formed linked list after the deletion. Example: Input: 1 <-> 2 <-> 3 <-> 4 <-> 5Output: 1 <-> 2 <-> 4 <-> 5Below are the dif
4 min read
JavaScript Program to Delete an Image by Clicking on a Button
In this article, we will learn how to delete an image by clicking on a button in JavaScript. In web development, there are scenarios where you may want to provide users with the ability to delete an image by clicking on a button. This can be useful in various applications, such as image galleries or
2 min read
JavaScript Program to Remove Empty Node from Linked List
JavaScript allows us to remove the empty node from the linked list by traversing through the linked list and finding each empty node. There are several approaches available in JavaScript through which this can be achieved which are as follows: Table of Content Iterative ApproachRecursive ApproachIte
3 min read
JavaScript Program to Remove Loop in a Linked List
A linked list is a collection of nodes that contain both a data element and a reference (or pointer) to the next node in the collection. A common concern with linked lists is having loops and cycles. When one of the nodes in the linked list points towards any of its previous nodes leading to an infi
7 min read
JavaScript delete Operator
The delete operator in JavaScript removes properties from objects, including inherited ones, and creates holes in arrays without changing their length. If a deleted property holds an object with no other references, that object is automatically released by JavaScript's garbage collector over time.Sy
3 min read
JavaScript - delete vs splice
To delete the array elements in JavaScript we can use the delete operator as well as the splice method. The delete operator is used to remove the element from the array without shifting the elements while the array.splice() method is used to remove, replace, and add new elements to the respective ar
2 min read
JavaScript Map delete() Method
JavaScript Map.delete() method is used to delete the specified element among all the elements that are present in the map. The Map.delete() method takes the key that needs to be removed from the map, thus removing the element associated with that key and returning true. If the key is not present the
3 min read
JavaScript Set delete() Method
The delete() method in JavaScript Set deletes an element from the set and returns true if the element existed and was successfully deleted. Otherwise, it returns false if the element does not exist.Syntax:mySet.delete(value);Parameters:value: It is the value of the element that is to be deleted from
3 min read
How to Open a Link Without Clicking on it using JavaScript?
To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is
1 min read