Why not delete the node after insert to a new position?


  • 0
    9

    Many answers insert node to new position without actually delete it from its original position. Wouldn't this cause a problem?

    For example, it could results in a ring:

    At first, the list is:

    1 -- 3 -- 4 -- 7 -- 8 -- 5

    when we check the node with value 5 (the last node), we found it should be inserted between 4 and 7. This is a "insert" operation, that is included in many posted solutions. However, why we don't need to delete 5 from where it was, i.e. after node with value 8? By deletion, I mean to set the next pointer of 8 to null.

    1 -- 3 -- 4 -- 5 -- 7 -- 8 --
    | |
    |------------ |


  • 0
    S

    Yes, I also doubt about it, I hope anyone can come and solve it, when I add this deletion, the running time exceed, which makes me very confused!!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.