Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert elements in the middle of LinkedList using a ListIterator

I want to create an empty LinkedList and using a ListIterator, add Integers to the List by always inserting them in the middle of the List. How to do that most efficiently.

Thank you

like image 974
aretai Avatar asked Jan 26 '12 18:01

aretai


1 Answers

Inserting elements into a LinkedList at an index is inherently inefficient. Use an ArrayList or something else instead of LinkedList if you must insert at an index.

But if you need info on using ListIterators, look here:

http://www.java-examples.com/iterate-through-elements-java-linkedlist-using-listiterator-example

Or else you might just consider doing

myLL.add(i,val)

For more info see the Java API.

http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html

like image 139
varatis Avatar answered Oct 25 '22 00:10

varatis