Java Linked List search and delete method

后端 未结 3 1852
谎友^
谎友^ 2020-12-07 00:05

I have a project for computer science class and have everything done except for one method. The delete method. Basically I am making a linked list from user input and I need

3条回答
  •  时光取名叫无心
    2020-12-07 00:20

    Without spoon feeding you the answer. deletion is a bit like removing one link of a chain - you cut out the link and join up the two (new) ends.

    So, deleting "B" would mean changing

    A --> B --> C --> D
    

    to this

    A --> C --> D
    


    In pseudo code, the algorithm would be:

    • start the algorithm with the first node
    • check if it is the one you want to delete
    • if not, go to the next node and check again (go back to the previous step)
    • if so, make the next node of the previous node the next node of this node
    • remove the reference from this node to the next node

提交回复
热议问题