Given the head of a singly linked list and a position (1-based index), delete the node at that position and return the updated head of the linked list.
Note: Position will be valid (i.e, 1 <= position <= linked list length)
Example:
Input: position = 1
Output: 2-> 3 -> 1-> 7 -> nullptr Explanation: After deleting the node at the 1st position (1-base indexing), the linked list is as
Input: position = 5
Output: 2 -> 3 -> 4 -> 5 -> nullptr Explanation: After deleting the node at 5th position (1-based indexing), the linked list is as
[Approach] Single Traversal Deletion - O(n) Time and O(1) Space
Deletion at a specified position in a linked list involves removing a node from a specific index/position, which can be the first, middle, or last node.
To perform the deletion, If the position is 1, we update the head to point to the next node and delete the current head. For other positions, we traverse the list to reach the node just before the specified position. If the target node exists, we adjust the next of this previous node to point to next of next nodes, which will result in skipping the target node.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intdata){this->data=data;this->next=nullptr;}};Node*deleteNode(Node*head,intposition){Node*temp=head;// Head is to be deletedif(position==1){head=temp->next;deletetemp;returnhead;}// Traverse to the node // before the one to be deletedNode*prev=nullptr;for(inti=1;i<position;i++){prev=temp;temp=temp->next;}// Delete the node at the positionprev->next=temp->next;deletetemp;returnhead;}voidprintList(Node*head){while(head!=nullptr){cout<<head->data<<" -> ";head=head->next;}cout<<"nullptr"<<endl;}intmain(){Node*head=newNode(1);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(4);intposition=3;head=deleteNode(head,position);printList(head);return0;}
Java
classGfG{staticclassNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}staticNodedeleteNode(Nodehead,intposition){Nodetemp=head;// Head is to be deletedif(position==1){head=temp.next;returnhead;}// Traverse to the node before // the one to be deletedNodeprev=null;for(inti=1;i<position;i++){prev=temp;temp=temp.next;}// Delete the node at the positionprev.next=temp.next;returnhead;}staticvoidprintList(Nodehead){while(head!=null){System.out.print(head.data+" -> ");head=head.next;}System.out.println("nullptr");}publicstaticvoidmain(String[]args){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);intposition=3;head=deleteNode(head,position);printList(head);}}
Python
classNode:def__init__(self,data):self.data=dataself.next=NonedefdeleteNode(head,position):temp=head# Head is to be deletedifposition==1:head=temp.nextreturnhead# Traverse to the node before # the one to be deletedprev=Noneforiinrange(1,position):prev=temptemp=temp.next# Delete the node at the positionprev.next=temp.nextreturnheaddefprintList(head):whileheadisnotNone:print(f"{head.data} -> ",end="")head=head.nextprint("nullptr")if__name__=="__main__":head=Node(1)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(4)position=3head=deleteNode(head,position)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{staticNodedeleteNode(Nodehead,intposition){Nodetemp=head;// Head is to be deletedif(position==1){head=temp.next;returnhead;}// Traverse to the node before// the one to be deletedNodeprev=null;for(inti=1;i<position;i++){prev=temp;temp=temp.next;}// Delete the node at the positionprev.next=temp.next;returnhead;}staticvoidPrintList(Nodehead){while(head!=null){Console.Write(head.data+" -> ");head=head.next;}Console.WriteLine("nullptr");}staticvoidMain(){Nodehead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);intposition=3;head=deleteNode(head,position);PrintList(head);}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}functiondeleteNode(head,position){lettemp=head;// Head is to be deletedif(position===1){head=temp.next;returnhead;}// Traverse to the node before the one to be deletedletprev=null;for(leti=1;i<position;i++){prev=temp;temp=temp.next;}// Delete the node at the positionprev.next=temp.next;returnhead;}functionprintList(head){letcurr=head;while(curr!==null){process.stdout.write(curr.data+" -> ");curr=curr.next;}console.log("nullptr");}// Driver Codelethead=newNode(1);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(4);letposition=3;head=deleteNode(head,position);printList(head);