Remove all nodes in linked list

前端 未结 4 1179
清酒与你
清酒与你 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:31

    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;
    }
    

    The logic for this would be more correct if it is this way.

    After the statement

    free(temp1);
    

    Add the condition

    if (head -> next != NULL)
        temp1 = head->next;
    

    Since after deleting the last node there is no point in reassigning the address of head pointer to temp1.

提交回复
热议问题