Java: remove all items from a linked list

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

Say I can do it in C

 void DeleteAllElement( ListElement **head ) { ListElement *deleteMe = *head; while( deleteMe ) { ListElement *next = deleteMe->next; delete deleteMe; deleteMe = next; } *head = NULL; } 

thanks

+4
source share
4 answers

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

myList.headNode = null;

So let's say we have a LinkedList class that 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 do not make private head node private, we can simply execute the first piece of code that I posted.

+11
source

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 implementation of the list, this can be terribly effective. For ArrayList this will be better:

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

Another alternative would be to iterate over the list and call Iterator.remove() for each element ... also an optional operation. (But then again, this can be terribly inefficient for some list implementations.)

If you are talking about a custom class of linked lists, then the answer depends on how you declared the structure of the internal structures of the list.


I suspect that if interviewers mentioned the clear() method, they expected a response in the context of a standard Java collection environment ... not for a class of related classes.

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

or see this example

+1
source

Reading the actual code is helpful. I suggest you look.

For a stand-alone list, which you can simply do as @bdares, suggests, when you look at the actual code for java.util.LinkedList (this is what most developers use), the answer is quite 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++; } 

The first thing to note; This is a double-linked list for forward and backward crawls, and it aggressively clears all links. I don’t know why this is done, because the GC will clear them in some way, and modCount will receive any changes in another thread. In fact, it should really execute 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; } 
+1
source

Source: https://habr.com/ru/post/1348035/


All Articles