The idea is to copy the data of the next node into the given node and then delete the next node. This removes the given value from the linked list without needing access to the head pointer. Since the given node is never the last node, this operation is always possible.
Working of Approach:
Store the next node of the given node.
Copy the next node's data into the given node.
Update the given node's next pointer to skip the next node.
Delete the skipped node.
The linked list size decreases by one.
Let us understand with an example: Input: head = 10 -> 20 -> 4 -> 30, x = 20
Store the next node (4) in a temporary pointer.
Copy the data of the next node (4) into x. The list becomes: 10 -> 4 -> 4 -> 30.
Update the next pointer of x to skip the copied node. The list becomes: 10 -> 4 -> 30.
Delete the skipped node (4), reducing the size of the linked list by one.
The final linked list is: 10 -> 4 -> 30.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to delete the given nodevoiddeleteNode(Node*x){// Store the next nodeNode*temp=x->next;// Copy next node's datax->data=temp->data;// Skip the next nodex->next=temp->next;// Delete the skipped nodedeletetemp;}// Function to print the linked listvoidprintList(Node*head){while(head!=nullptr){cout<<head->data;if(head->next)cout<<" -> ";head=head->next;}cout<<endl;}intmain(){Node*head=newNode(10);head->next=newNode(20);head->next->next=newNode(4);head->next->next->next=newNode(30);Node*x=head->next;deleteNode(x);printList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGFG{// Function to delete the given nodestaticvoiddeleteNode(Nodex){// Store the next nodeNodetemp=x.next;// Copy next node's datax.data=temp.data;// Skip the next nodex.next=temp.next;}// Function to print the linked liststaticvoidprintList(Nodehead){while(head!=null){System.out.print(head.data);if(head.next!=null)System.out.print(" -> ");head=head.next;}System.out.println();}publicstaticvoidmain(String[]args){Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(4);head.next.next.next=newNode(30);Nodex=head.next;deleteNode(x);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=None# Function to delete the given nodedefdeleteNode(x):# Store the next nodetemp=x.next# Copy next node's datax.data=temp.data# Skip the next nodex.next=temp.next# Delete the skipped nodedeltemp# Function to print the linked listdefprintList(head):whileheadisnotNone:print(head.data,end="")ifhead.nextisnotNone:print(" -> ",end="")head=head.nextprint()if__name__=='__main__':head=Node(10)head.next=Node(20)head.next.next=Node(4)head.next.next.next=Node(30)x=head.nextdeleteNode(x)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGFG{// Function to delete the given nodestaticvoiddeleteNode(Nodex){// Store the next nodeNodetemp=x.next;// Copy next node's datax.data=temp.data;// Skip the next nodex.next=temp.next;}// Function to print the linked liststaticvoidPrintList(Nodehead){while(head!=null){Console.Write(head.data);if(head.next!=null)Console.Write(" -> ");head=head.next;}Console.WriteLine();}staticvoidMain(){Nodehead=newNode(10);head.next=newNode(20);head.next.next=newNode(4);head.next.next.next=newNode(30);Nodex=head.next;deleteNode(x);PrintList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}// Function to delete the given nodefunctiondeleteNode(x){// Store the next nodeconsttemp=x.next;// Copy next node's datax.data=temp.data;// Skip the next nodex.next=temp.next;// Delete the skipped nodetemp.next=null;}// Function to print the linked listfunctionprintList(head){letcurrent=head;while(current!==null){process.stdout.write(current.data.toString());if(current.next!==null){process.stdout.write(" -> ");}current=current.next;}console.log();}// Driver Codeconsthead=newNode(10);head.next=newNode(20);head.next.next=newNode(4);head.next.next.next=newNode(30);constx=head.next;deleteNode(x);printList(head);
Output
10 -> 4 -> 30
Note: This technique works only when the given node is not the last node. If the last node needs to be deleted, we must have access to either the previous node or the head pointer to update the previous node's next pointer. Since this problem guarantees that x is not the last node, the above approach always works.