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

前端 未结 24 1352
陌清茗
陌清茗 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条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 20:51

    Given

    A -> B -> C -> D

    and a pointer to, say, item B, you would delete it by
    1. free any memory belonging to members of B
    2. copy the contents of C into B (this includes its "next" pointer)
    3. delete the entire item C

    Of course, you'll have to be careful about edge cases, such as working on lists of one item.

    Now where there was B, you have C and the storage that used to be C is freed.

提交回复
热议问题