Delete a Node in Single Linked List | Practice | GeeksforGeeks

Given a singly linked list and an integer x.Delete xth node from the singly linked list. 
Example 1: 
Input: 1 -> 3 -> 4 
       x = 3
Output: 1 -> 3
Explanation:
After deleting the node at 3rd
position (1-base indexing), the
linked list is as 1 -> 3. 

Example 2:
Input: 1 -> 5 -> 2 -> 9 
x = 2
Output: 1 -> 2 -> 9
Explanation: 
After deleting the node at 2nd
position (1-based indexing), the
linked list is as 1 -> 2 -> 9.
 
Your task: Your task is to complete the method deleteNode() which takes two arguments: the address of the head of the linked list and an integer x. The function returns the head of the modified linked list.
Constraints:
1 <= T <= 300
2 <= N <= 100
1 <= x <= N
 
0
mannukhurana1039719 hours ago
java 
 
Node deleteNode(Node head, int x)
    {
        // time complexity: O(x): 0.62/1.88
        // space complexity: O(1)
	    Node prev = head;
	    Node temp = head;
	    
	    if(x==1) 
	        return head.next;
	    
	    while(temp!=null && --x>=0){
	        if(x==0){
	            prev.next = temp.next;
	            return head;
	        }
	        prev = temp;
	        temp = temp.next;
	    }
	    return head;
    }
0
sarthakpandey20021 week ago
EASY C++ SOLUTION
Node* deleteNode(Node *head,int x){   //Your code here   Node*a=head;   int count=1;   if(x==1)   return head->next;   while(a!=NULL){       ++count;       if(count==x){           a->next=a->next->next;       }       //++count;       a=a->next;   }   return head;} 
0
harshit8491 week ago
Simplest recursive Technique to solve this problem.
/*You are required to complete below method*/class GfG{   Node deleteNode(Node head, int x)   {// Your code hereif(head==null) return head;if(x==1)return head.next;head.next = deleteNode(head.next,x-1);return head;   }} 
0
sudeepgarg6711 week ago
Node* deleteNode(Node *head,int x){   if (x == 1){      Node* grb = head;      head = head->next;      delete grb;      return head;  }   Node* temp = head;   int count = 2;   while(count!=x){       temp=temp->next;       count++;   }   Node* ans = temp->next;   temp->next = temp->next->next;   delete ans;   return head;}
0
swapniltayal4221 week ago
Node* deleteNode(Node *head,int x){      if (x == 1){       Node* grb = head;       head = head->next;       delete grb;       return head;   }      Node* temp = head;   int count = 0;      while(count < x-2){       temp = temp->next;       count ++;   }      Node* grb = temp->next;   temp->next = temp->next->next;   delete grb;   return head;}
0
alokkumar90192 weeks ago
Node* deleteNode(Node *head,int k)
{
   //Your code here
   if(k==1){
       Node *temp=head->next;
       delete(head);
       return temp;
   } else{
       Node *curr=head;
       for(int i=0; i<k-2; i++){
           curr=curr->next;
       }
       Node *temp=curr->next;
       curr->next=curr->next->next;
       delete(temp);
       return head;
   }
   
   
}
0
kininarasimha2 weeks ago
//javascript
class Solution { deleteNode(head,x){   //code here   if(x===1){       head = head.next;   } else {       let prev = head;       for(let i =1;i<x-1;i++){           prev = prev.next;       }       prev.next = prev.next.next;   }   return head; }}
+1
code7225
This comment was deleted.
0
tthakare732 weeks ago
//java Solution
class GfG{
    Node deleteNode(Node head, int x){
	// Your code here	
	    int i = 1;
	    Node pre = null, current = head;
	    if(x == 1) return head.next;
	    while(i < x && current != null){
	        pre = current;
	        current = current.next;
	        i++;
	    }
	    if(current != null) pre.next = current.next; 
	    return head;
    }
}

0
hharshit81183 weeks ago
Time Complexity = O(N)
Node* deleteNode(Node *head,int x){   if(x == 1){       Node* temp = head;       head = head->next;       delete(temp);       return head;   }   int i = 1;   Node* ptr = head;   while(i<=x-2){       ptr = ptr->next;       i++;   }       Node* temp = ptr->next;       ptr->next = temp->next;       delete(temp);       return head;   return NULL;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.