[Naive Approach] Store Nodes in an Array - O(n) Time and O(n) Space
The idea is to traverse the linked list and store the address of every node in an array. This allows direct access to the node at position x. Then, update the links to remove the node and delete it.
Working of Approach:
Traverse the linked list and store all node pointers in an array.
If x == 1, delete the head node.
Otherwise, access the previous and current nodes using the array.
Update the previous node's next pointer.
Delete the node and return the updated head.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*deleteNode(Node*head,intx){vector<Node*>nodes;// Store all node pointersNode*curr=head;while(curr!=nullptr){nodes.push_back(curr);curr=curr->next;}// Delete the head nodeif(x==1){Node*temp=head;head=head->next;deletetemp;returnhead;}Node*prev=nodes[x-2];Node*delNode=nodes[x-1];// Remove the nodeprev->next=delNode->next;deletedelNode;returnhead;}// Function to print the linked listvoidprintList(Node*head){while(head!=nullptr){cout<<head->data;if(head->next)cout<<" -> ";head=head->next;}cout<<endl;}intmain(){// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100Node*head=newNode(2);head->next=newNode(5);head->next->next=newNode(7);head->next->next->next=newNode(8);head->next->next->next->next=newNode(99);head->next->next->next->next->next=newNode(100);intx=6;head=deleteNode(head,x);printList(head);return0;}
Java
importjava.util.ArrayList;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodedeleteNode(Nodehead,intx){ArrayList<Node>nodes=newArrayList<>();// Store all node pointersNodecurr=head;while(curr!=null){nodes.add(curr);curr=curr.next;}// Delete the head nodeif(x==1){Nodetemp=head;head=head.next;temp=null;returnhead;}Nodeprev=nodes.get(x-2);NodedelNode=nodes.get(x-1);// Remove the nodeprev.next=delNode.next;delNode=null;returnhead;}// Function to print the linked listpublicstaticvoidprintList(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){// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100Nodehead=newNode(2);head.next=newNode(5);head.next.next=newNode(7);head.next.next.next=newNode(8);head.next.next.next.next=newNode(99);head.next.next.next.next.next=newNode(100);intx=6;head=deleteNode(head,x);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefdeleteNode(head,x):nodes=[]# Store all node pointerscurr=headwhilecurrisnotNone:nodes.append(curr)curr=curr.next# Delete the head nodeifx==1:temp=headhead=head.nextdeltempreturnheadprev=nodes[x-2]delNode=nodes[x-1]# Remove the nodeprev.next=delNode.nextdeldelNodereturnhead# Function to print the linked listdefprintList(head):whileheadisnotNone:print(head.data,end="")ifhead.nextisnotNone:print(" -> ",end="")head=head.nextprint()if__name__=="__main__":# Create linked list:# 2 -> 5 -> 7 -> 8 -> 99 -> 100head=Node(2)head.next=Node(5)head.next.next=Node(7)head.next.next.next=Node(8)head.next.next.next.next=Node(99)head.next.next.next.next.next=Node(100)x=6head=deleteNode(head,x)printList(head)
C#
usingSystem;usingSystem.Collections.Generic;publicclassNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodedeleteNode(Nodehead,intx){List<Node>nodes=newList<Node>();// Store all node pointersNodecurr=head;while(curr!=null){nodes.Add(curr);curr=curr.next;}// Delete the head nodeif(x==1){Nodetemp=head;head=head.next;temp=null;returnhead;}Nodeprev=nodes[x-2];NodedelNode=nodes[x-1];// Remove the nodeprev.next=delNode.next;delNode=null;returnhead;}// Function to print the linked listpublicstaticvoidprintList(Nodehead){while(head!=null){Console.Write(head.data);if(head.next!=null)Console.Write(" -> ");head=head.next;}Console.WriteLine();}publicstaticvoidMain(){// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100Nodehead=newNode(2);head.next=newNode(5);head.next.next=newNode(7);head.next.next.next=newNode(8);head.next.next.next.next=newNode(99);head.next.next.next.next.next=newNode(100);intx=6;head=deleteNode(head,x);printList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functiondeleteNode(head,x){letnodes=[];// Store all node pointersletcurr=head;while(curr!==null){nodes.push(curr);curr=curr.next;}// Delete the head nodeif(x===1){lettemp=head;head=head.next;temp=null;returnhead;}letprev=nodes[x-2];letdelNode=nodes[x-1];// Remove the nodeprev.next=delNode.next;delNode=null;returnhead;}// Function to print the linked listfunctionprintList(head){while(head!==null){process.stdout.write(head.data.toString());if(head.next!==null)process.stdout.write(" -> ");head=head.next;}console.log();}// Driver Code// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100lethead=newNode(2);head.next=newNode(5);head.next.next=newNode(7);head.next.next.next=newNode(8);head.next.next.next.next=newNode(99);head.next.next.next.next.next=newNode(100);letx=6;head=deleteNode(head,x);printList(head);
Output
2 -> 5 -> 7 -> 8 -> 99
[Expected Approach] Single Traversal Deletion - O(n) Time and O(1) Space
The idea is to traverse the linked list only once while keeping track of the previous node. When the node at position x is reached, update the previous node's next pointer to skip it and delete the node. If x is 1, simply update the head pointer.
Working of Approach:
If x == 1, delete the head node.
Traverse the list while maintaining both current and previous pointers.
Stop when the current node reaches position x.
Update the previous node's next pointer.
Delete the current node and return the updated head.
Let us understand with an example: Input: x = 6,
The linked list is 2 -> 5 -> 7 -> 8 -> 99 -> 100 and x = 6, so the head is not deleted.
Traverse the list while updating prev and temp. After the loop, prev points to 99 and temp points to 100.
Update prev->next to temp->next (nullptr), removing 100 from the linked list.
Delete the node pointed to by temp.
The updated linked list becomes
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};Node*deleteNode(Node*head,intx){Node*temp=head;// Case 1: Head is to be deletedif(x==1){head=temp->next;deletetemp;returnhead;}// Case 2: Traverse to the node// before the one to be deletedNode*prev=nullptr;for(inti=1;i<x;i++){prev=temp;temp=temp->next;}// Delete the node at position xprev->next=temp->next;deletetemp;returnhead;}// Function to print the linked listvoidprintList(Node*head){while(head!=nullptr){cout<<head->data;if(head->next)cout<<" -> ";head=head->next;}cout<<endl;}intmain(){// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100Node*head=newNode(2);head->next=newNode(5);head->next->next=newNode(7);head->next->next->next=newNode(8);head->next->next->next->next=newNode(99);head->next->next->next->next->next=newNode(100);intx=6;head=deleteNode(head,x);printList(head);return0;}
Java
classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}publicclassGFG{publicstaticNodedeleteNode(Nodehead,intx){Nodetemp=head;// Case 1: Head is to be deletedif(x==1){head=temp.next;temp=null;returnhead;}// Case 2: Traverse to the node// before the one to be deletedNodeprev=null;for(inti=1;i<x;i++){prev=temp;temp=temp.next;}// Delete the node at position xprev.next=temp.next;temp=null;returnhead;}publicstaticvoidprintList(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){// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100Nodehead=newNode(2);head.next=newNode(5);head.next.next=newNode(7);head.next.next.next=newNode(8);head.next.next.next.next=newNode(99);head.next.next.next.next.next=newNode(100);intx=6;head=deleteNode(head,x);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=NonedefdeleteNode(head,x):temp=head# Case 1: Head is to be deletedifx==1:head=temp.nextdeltempreturnhead# Case 2: Traverse to the node# before the one to be deletedprev=Noneforiinrange(1,x):prev=temptemp=temp.next# Delete the node at position xprev.next=temp.nextdeltempreturnheaddefprintList(head):whileheadisnotNone:print(head.data,end='')ifhead.nextisnotNone:print(' -> ',end='')head=head.nextprint()if__name__=='__main__':# Create linked list:# 2 -> 5 -> 7 -> 8 -> 99 -> 100head=Node(2)head.next=Node(5)head.next.next=Node(7)head.next.next.next=Node(8)head.next.next.next.next=Node(99)head.next.next.next.next.next=Node(100)x=6head=deleteNode(head,x)printList(head)
C#
classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGFG{staticNodedeleteNode(Nodehead,intx){Nodetemp=head;// Case 1: Head is to be deletedif(x==1){head=temp.next;temp=null;returnhead;}// Case 2: Traverse to the node// before the one to be deletedNodeprev=null;for(inti=1;i<x;i++){prev=temp;temp=temp.next;}// Delete the node at position xprev.next=temp.next;temp=null;returnhead;}staticvoidprintList(Nodehead){while(head!=null){System.Console.Write(head.data);if(head.next!=null)System.Console.Write(" -> ");head=head.next;}System.Console.WriteLine();}staticvoidMain(string[]args){// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100Nodehead=newNode(2);head.next=newNode(5);head.next.next=newNode(7);head.next.next.next=newNode(8);head.next.next.next.next=newNode(99);head.next.next.next.next.next=newNode(100);intx=6;head=deleteNode(head,x);printList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}functiondeleteNode(head,x){lettemp=head;// Case 1: Head is to be deletedif(x===1){head=temp.next;temp=null;returnhead;}// Case 2: Traverse to the node// before the one to be deletedletprev=null;for(leti=1;i<x;i++){prev=temp;temp=temp.next;}// Delete the node at position xprev.next=temp.next;temp=null;returnhead;}functionprintList(head){while(head!==null){process.stdout.write(head.data.toString());if(head.next!==null){process.stdout.write(" -> ");}head=head.next;}console.log();}// Driver Code// Create linked list:// 2 -> 5 -> 7 -> 8 -> 99 -> 100lethead=newNode(2);head.next=newNode(5);head.next.next=newNode(7);head.next.next.next=newNode(8);head.next.next.next.next=newNode(99);head.next.next.next.next.next=newNode(100);letx=6;head=deleteNode(head,x);printList(head);