ProAnswers.org

algorithm Delete a node from a linked list

algorithm Delete a node from a linked list

typedef struct node{

	    int data;

	    struct node *next;

	}node;

	 

	/* This function deletes a node

	   at index i and returns a pointer

	   to the new list formed */

	 

	node *delete_node(node *head, int index){

	    node *move, *delt;

	    int i = 0;

	    delt = move = head;

	    if(index == 1)

	    /*deleting from the beginning of the  list*/

	    {

	        move = move->next;

	        head->next = NULL;

	        head = move;

	        return head;

	    }

	    for(; i

	        delt = delt->next;

	    while(move->next != delt && move->next!=NULL)

	        move = move->next;

	    move->next = delt->next;

	    delt->next = NULL;

	    return head;

	}