java - how to delete a node from linkedlist?

前端 未结 4 673
长发绾君心
长发绾君心 2020-12-11 09:41

This code is a table that has an option to Inert name, delete, show, and quit .

this code run\'s well but my only problem is on how to delete a chosen name in a nod

相关标签:
4条回答
  • 2020-12-11 09:52

    To delete Node you actually need to update it's previous node's in to be deleting Node's in, and the left alone Node will eventually get garbage collected.

    Just one catch if node to be deleted is the root node then update root node.

    private Node delete(Node root, String data)
    {
        //in case list is empty then return
        if(root==null) return n;
        //in case node to be deleted is root then just return next as new root
        if (root.name.equals(data)) return root.in;
    
        Node curr = root;
        while(curr.in!=null)
        {
            if (curr.in.name.equals(data))
            {
                //curr.in's referenced Node will be garbage collected (or run some cleanup manually on it)
                curr.in = curr.in.in;
                //we are done and root is same
                return root;
            }
            curr = curr.in;
        }
        //if here then not found and nothing changed
        return root;
    }
    
    0 讨论(0)
  • 2020-12-11 09:54
    while (node != null) {
    
                if (node.getNext() == null || head.getNext() == null) {
                    break;
    
                } else if (head.getData() == data) {
                    head = head.getNext();
                } else if (node.getNext().getData()==null&&data==null||node.getNext().getData().equals(data)) {
                    node.setNext(node.getNext().getNext());
                } else {
                    node = node.getNext();
                }
            }
        }
    
        return head;
    
    0 讨论(0)
  • 2020-12-11 10:01

    Are you trying to remove the name from the node, or remove the node from the list? To remove the node from the list, use the LinkedList.remove(int index) method. You'll need to find the index of the node you want to remove first.

    [edit] Like the others have said, you should try to solve the problem yourself, but here's a hint: you can access each node with LinkedList.get(int index). You can get the length of the list with LinkedList.size(). This is probably a good place for a "for" loop.

    0 讨论(0)
  • 2020-12-11 10:07

    We could code this for you, but that misses the point.

    Instead, I'm going to suggest that you draw the linked-list data structure on paper using boxes for the list nodes and fields of the nodes, and arrows for the pointers / references. Then draw more boxes for your algorithm's local variables ... and "hand execute" it. That will help you visualize what your code should be doing.

    Once you have done this kind of thing a few times, you will be able to visualize in your head ...


    can you please give me a sample ?

    Sorry, but No. You will learn more by working it out for yourself. See above.

    0 讨论(0)
提交回复
热议问题