Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires a pointer to the head node which contradicts the problem statement. Show
The fast solution is to copy the data from the next node to the node to be deleted and delete the next node. Something like this: It is important to note that this approach will only work if it is guaranteed that the given pointer does not point to the last node. Because if it is the last node, then you don’t have a next node to copy the data from. struct Node *temp = node_ptr->next; node_ptr->data = temp->data; node_ptr->next = temp->next; free(temp);Below is the implementation of the above code: C++
C
Java
Python3
C#
Javascript
Output Before deleting 1 12 1 4 1 After deleting 12 1 4 1 To make this solution work, we can mark the end node as a dummy node. But the programs/functions that are using this function should also be modified.
Article Tags :
Linked List
Practice Tags :
Linked List Problem StatementThe problem “Delete a Node from linked list without head pointer” states that you have a linked list with some nodes. Now you want to delete a node but you don’t have its parent node address. So delete this node. ExamplePin 2->3->4->5->6->7 Node to be deleted: 42->3->5->6->7Explanation: Since we wanted to delete the node with value 4. We removed it from the linked list and are left with a list that is mentioned in the output. Approach to delete node without head pointerLinked List is a linear data structure that has a huge advantage over an array is that it can vary in size. If you want to add an element in a linked list, you can add it in O(1) time. Considering that you are inserting the element in the first place. But if you had to do the same in a normal array. It will cost you O(N) time to do so. Thus it is favorable to use a linked list over an array in real-world applications. And linked list achieves all of this because it does not store the data as a contiguous chunk. It stores each element at some random location. But then how does it maintain order? The answer to this question is, the linked list takes the usage of the pointer. Each node points to another node in the linked list and this way we do not have to allocate a single chunk changing which in turn would have required many computations. See also Merge K Sorted Linked Lists If head pointer was givenNow the problem asks us to delete a node. Generally, we are given a node’s address and are required to delete the next node in the linked list. But here we are required to delete a node whose address is given to us. One way to solve this problem is to first traverse the whole linked list. Then store the parent of each node and break the loop when you are at a node that is provided as input. This way in the end we have the parent of the node to be deleted. So now the problem is changed to the simple deletion of a node in the linked list whose parent’s address is given. But doing this will cost us O(N) operations. But this way can be used only when you have the head pointer. SolutionThe approach for the problem “Delete a Node from linked list without head pointer” would be to swap the data of the node to be deleted and the next node in the linked list. Each node in the linked list stores which node is next. So this operation has only O(1) time complexity. Now when the data has been swapped. Once again, we have changed the problem to the deletion of the node whose parent’s address is given. So now it’s easier for us to solve the problem. But there is one edge case when we have to delete the end node but we won’t be able to delete that. Because there is no next node. See also Average Salary Excluding the Minimum and Maximum Salary Leetcode Solution Delete a Node from linked list without head pointer in C++C++Server Side ProgrammingProgramming In this tutorial, we are going to learn how to delete a node without head pointer in a singly linked list. Let's see the steps to solve the problem.
Deleting a node from a linked list without head pointerIn this article, we are going to see how to delete a node in a linked list without having head pointer? Problem statement: Write a program to delete a node in a linked list where the head pointer to the node is not given, only the address of the node to be deleted is provided. Example: Basic list: 4->5->1->6->7->NULL Node to be deleted: 1 After deletion: 4->5->6->7->NULLSolution: In this problem we don’t have head to the list, we only have address of the node to be deleted that is pointer to the node 1 So actually we have access to the part: 1->6->7->NULLSo we can simply swap the node values one by one: And then make the last node NULL 6->7->NULLSo the total linked list actually begins: 4->5->6->7->NULLAlgorithm: 1. Declare 'temp' to store node values 2. Declare 'ListNode* pre' and initialize to address of node to be deleted; 3. While node->next //swap value only with the next node temp =node->data; node->data=(node->next)->data; (node->next)->data=temp; // Go to the next node node=node->next; END WHILE 4. Make the last node NULL ListNode *fast=pre->next; //check what pre is at step no 2 While (fast->next) pre=pre->next; fast=fast->next; END WHILE pre->next=NULL;ADVERTISEMENT C++ implementation #include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode* next; }; ListNode* creatnode(int d){ ListNode* temp=(ListNode*)malloc(sizeof(ListNode)); temp->val=d; temp->next=NULL; return temp; } void traverse(ListNode* head){ ListNode* current=head; // current node set to head int count=0; // to count total no of nodes printf("displaying the list\n"); //traverse until current node isn't NULL while(current!=NULL){ count++; //increase node count if(current->next) printf("%d->",current->val); else printf("NULL\n"); current=current->next; // go to next node } printf("total no of nodes : %d\n",count); } void deleteNode(ListNode* node) { //deleting node without head pointer int temp; ListNode* pre=node; while(node->next){ pre = node; node->val = (node->next)->val; node=node->next; } pre->next=NULL; free(node); } int main(){ ListNode* head=creatnode(4); head->next=creatnode(5); head->next->next=creatnode(1); head->next->next->next=creatnode(6); head->next->next->next->next=creatnode(7); head->next->next->next->next->next=creatnode(8); ListNode* todelete=head->next->next; //1 cout<<"deleting node with value 1\n"; cout<<"before deletion:\n"; traverse(head); deleteNode(todelete); cout<<"after deletion:\n"; traverse(head); return 0; }Output deleting node with value 1 before deletion: displaying the list 4->5->1->6->7->NULL total no of nodes : 6 after deletion: displaying the list 4->5->6->7->NULL total no of nodes : 5ADVERTISEMENT Preparation Aptitude Questions MCQs Find Output Programs What's New
ADVERTISEMENT Top Interview Coding Problems/Challenges!
Comments and Discussions! Please enable JavaScript to view the comments powered by Disqus. ADVERTISEMENT How do you delete a node from singly linked list when you are given only the node?A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires a pointer to the head node which contradicts the problem statement. The fast solution is to copy the data from the next node to the node to be deleted and delete the next node. How do you delete a node in a linked list Python?To delete a node from the linked list, we need to do the following steps.
|