开发者

Java:Delete all the elements from Linked List

开发者 https://www.devze.com 2023-02-24 22:44 出处:网络
In Java, how to remove all the elements in linked list without using already available clear() method?This exercise was inspired by a question received in a phone interview.

In Java, how to remove all the elements in linked list without using already available clear() method? This exercise was inspired by a question received in a phone interview.

Say I can do this in C

void DeleteAllElement( ListElement **hea开发者_运维百科d ) {  
    ListElement *deleteMe = *head;  
    while( deleteMe )  {  
        ListElement *next = deleteMe->next;  
        delete deleteMe;  
        deleteMe = next;  
    }  
    *head = NULL;  
}

Thanks


Java has automatic garbage collection, so you just need to set the Head reference to null:

myList.headNode = null;

So, let's say we have the class LinkedList, which also has a resetList function...

public class LinkedList{
     private Node head;
     public Node find(Key k){ ... }
     public void add(Node n){ ... }
     ...
     public void reset(){ head = null;}
     public static void reset(LinkedList l){l.reset();}
}

If we are not making the head node private, we could simply execute the first code snippet I posted.


If you are talking about an instance of java.util.LinkedList:

    while (!linkedlist.isEmpty()) {
        linkedlist.removeFirst();
    }

If you are talking about an instance of any java.util.List:

    while (!list.isEmpty()) {
        list.remove(0);
    }

bearing in mind that remove is an optional operation. However, depending on the list implementation, that could be horribly in efficient. For an ArrayList this would be better:

    while (!list.isEmpty()) {
        list.remove(list.size() - 1);
    }

Another alternative would be to iterate the list and call Iterator.remove() on each element ... also an optional operation. (But once again, that could be horribly inefficient for some list implementations.)

If you are talking about a custom linked list class, then the answer depends on the way you have declared the list classes internal data structures.


I suspect that if the interviewers mentioned the clear() method, they were expecting an answer in the context of the standard Java collection framework ... not a custom linked list class.


Reading the actual code is useful. I suggest you have a look.

For a singly linked list you can just do as @bdares, suggests when you look at the actual code for java.util.LinkedList (Which is what most developers use), the answer is rather different.

public void clear() {
    Entry<E> e = header.next;
    while (e != header) {
        Entry<E> next = e.next;
        e.next = e.previous = null;
        e.element = null;
        e = next;
    }
    header.next = header.previous = header;
    size = 0;
    modCount++;
}

First thing to note; this is doubly linked list for forward and backward traversal and it agressively clears all the references. Not sure why it does this to be honist because the GC will clear thes eup any way and the modCount will pick up any changes in another thread. In fact it should really perform the modCount first.

For comparison, here is ArrayList.clear();

public void clear() {
    modCount++;

    // Let gc do its work
    for (int i = 0; i < size; i++)
        elementData[i] = null;

    size = 0;
}


for(i=0;i<linkedlist.size;i++){
  linkedlist.removeFirst();
}

or see this example.

0

精彩评论

暂无评论...
验证码 换一张
取 消