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

前端 未结 24 1387
陌清茗
陌清茗 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 21:10

    You could do delayed delinking where you set nodes to be delinked out of the list with a flag and then delete them on the next proper traversal. Nodes set to be delinked would need to be properly handled by the code that crawls the list.

    I suppose you could also just traverse the list again from the beginning until you find the thing that points to your item in the list. Hardly optimal, but at least a much better idea than delayed delinking.

    In general, you should know the pointer to the item you just came from and you should be passing that around.

    (Edit: Ick, with the time it took me to type out a fullish answer three gazillion people covered almost all the points I was going to mention. :()

提交回复
热议问题