Delete Node in LL

 /****************************************************************

 

    Following is the class structure of the Node class:


        class Node

        {

        public:

        int data;

        Node *next;

        Node(int data)

        {

        this->data = data;

        this->next = NULL;

        }

        };


*****************************************************************/

int length(Node *head)

{

    //Write your code here

    int count=0;

    Node* temp=head;

    while(temp!=NULL){

        count++;

        temp=temp->next;

    }

    

    return count;

}


Node *deleteNode(Node *head, int i)

{

    //Write your code here

   // Node*n=new node(data);

    int len=length(head);

    if(i>len-1){

        return head;

    }

    Node*temp=head;

    int count=0;

    if (i==0){

        head=head->next;

        return head;

    }

    while(count<i-1 && temp!=NULL){

        temp=temp->next;

        count++;

    }

    if(temp!=NULL){

        Node*a=temp->next;

        Node*b=a->next;

        temp->next=b;

        delete a;

        

    }

    return head;

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings