Inserting a node into a linked list in constant-time?

前端 未结 7 1017
别跟我提以往
别跟我提以往 2021-01-24 09:09

I\'m working on an assignment that is telling me to assume that I have a singly linked list with a header and tail nodes. It wants me to insert an item y before position p. Ca

7条回答
  •  感动是毒
    2021-01-24 09:34

    What you are not doing is linking the element that was before p prior to insertion of y to y. So while y is inserted before p, no one is pointing to y now (at-least not in the code snipped you showed).

    You can only insert in constant time if you know the positions of the elements between which you have to insert y. If you have to search for that position, then you can never have a constant time insertion in a single link list.

提交回复
热议问题