为已删除的节点引用分配null是否会释放分配的内存?

时间:2018-12-08 11:23:59

标签: linked-list

在下面的代码片段中,将指针引用取消关联到已删除的节点后,将其指向空释放分配的内存吗,或者这样做好吗?

public Node deleteNode(int data) {
        if (head == null) {
            System.out.println("List is empty");
            return null;
        }
        if (head.next == null && head.data != data) {
            System.out.println("List NOt having the Node with data");
            return null;
        }

        Node curr = head.next;
        Node prev = head;
        while (curr != null && curr.data != data) {
            prev = curr;
            curr = curr.next;
        }

        if (curr == null) {
            System.out.println("List NOt having the Node with data");
            return null;
        }

        /*
         * Node temp = prev.next; //1---> 2--->3 Node temp1= curr.next;
         */
        prev.next = curr.next;
        /*
         * temp = null; temp1 = null;
         */

        return head;
    }

0 个答案:

没有答案