Remove all nodes in linked list

前端 未结 4 1137
清酒与你
清酒与你 2021-01-29 06:03

I have a linked list contains 3 nodes like the image shown: \"enter

There is a head pointe

4条回答
  •  离开以前
    2021-01-29 06:16

    To delete all nodes except the first node, you can try below code.

    temp1 = head->next;
    while(temp1!=NULL) // as I am considering tail->next = NULL
    {   
        head->next = temp1->next;
        temp1->next = NULL;
        free(temp1);
        temp1 = head->next;
    }
    

    This will delete all nodes except first one. But the data with the first node will remain as it is.

提交回复
热议问题