Delete a Node in Single Linked List

question–>
https://practice.geeksforgeeks.org/problems/delete-a-node-in-single-linked-list/1/?category[]=Linked%20List&category[]=Linked%20List&problemStatus=unsolved&difficulty[]=0&page=1&query=category[]Linked%20ListproblemStatusunsolveddifficulty[]0page1category[]Linked%20List
Code–>


Ir is showing segmentation fault

@tusharaggarwal272 are you there

Hello @sheikhhaji18 from where do you want to delete the node?

submitting in geeks for geeks

@sheikhhaji18 no i am asking from what point you want to delete the node?
i.e from front or at any in between point or from the end?

whereever the key is present,in ide it is working ,but in gfg it is showing segmentation fault

Hello @sheikhhaji18 your code is wrong because you have to delete that node number node that node value.
2
3
1 3 4
3
4
1 5 2 9
2
in this sample test case: you have to delete the third node and the in the sample test case you have to delete the second node.
make this change then your code will pass all the test cases:
Happy Learning!!

i didnot see it properly :face_with_head_bandage: :face_with_head_bandage:

Node* deleteNode(Node *head,int x)
{
    Node* temp=head;
    Node* prev=NULL;
    int i=1;
    while(temp!=NULL and i<x){
       
         prev=temp;
        temp=temp->next;
        i++;
    }
    if(temp==head and prev==NULL){
        if(temp->next==NULL){
            return NULL;
        }
        else{
        temp=head->next;
        head->next=NULL;
        delete(head);
        return temp;}
    }
    else if(temp->next==NULL ){
       if(head->next==NULL){
           return NULL;
       }
       else{
           delete(temp);
           prev->next=NULL;
           return head;
       }
        
    }
    else if(temp!=NULL and prev!=NULL){
        prev->next=temp->next;
        temp->next=NULL;
        delete(temp);
        return head;
    }
    else{
        return head;
    }
}

passed bro thanks and marked it resolved

@sheikhhaji18 happy to help you:
Happy Learning!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.