Deleting a middle node from a single linked list when pointer to the previous node is not available

前端 未结 24 1338
陌清茗
陌清茗 2020-11-29 20:42

Is it possible to delete a middle node in the single linked list when the only information available we have is the pointer to the node to be deleted and not the pointer to

24条回答
  •  -上瘾入骨i
    2020-11-29 21:18

    Considering below linked list

    1 -> 2 -> 3 -> NULL

    Pointer to node 2 is given say "ptr".

    We can have pseudo-code which looks something like this:

    struct node* temp = ptr->next;
    ptr->data = temp->data;
    ptr->next = temp->next;
    free(temp);
    

提交回复
热议问题