How to insert an element before the specified element in the linked list, so that the time complexity is n. For example I want to insert 100 before 7
LinkedList<Integer> linkedList = new LinkedList<>();
for (int i = 1; i <= 10; i++) {
linkedList.add(i);
}
I can do it like this
int index = linkedList.indexOf(7);
if (-1 != index) {
linkedList.add(index, 100);
}
But I have traversed the linked list twice by this way.
Actually we can do this just by traversing one time.
So how can I do that?
PS:just use LinkedList
2 Answers 2
There is a way to do this in one pass using a ListIterator
. A ListIterator
is like a regular Iterator
except that you can change the direction of iteration, and you can add
an element at the current position; see javadoc.
So the code goes something like this:
LinkedList<SomeType> list = ...
SomeType a, b = ..
// Insert 'b' before the first element equal to 'a' in 'list'
ListIterator<SomeType> iterator = list.listIterator(0);
while (iterator.hasNext()) {
SomeType e = iterator.next();
if (e.equals(a)) {
iterator.previous(); // returns 'e' again. But the real purpose
// is to reset the iteration position
// so that 'next()' would return 'e' again.
iterator.add(b); // inserts before 'next()'.
break;
}
}
The ListIterator::add
operation is an "optional" operation, but it is supported by LinkedList
. The LinkedList
javadoc say that the above won't cause a ConcurrentModificationException
.
How about this:
LinkedList<Integer> linkedList = new LinkedList<>();
int addNumber = 100, beforeTheNumber = 7;
for(int i = 1; i <= 10; i++) {
if(i == beforeTheNumber)
linkedList.add(addNumber);
linkedList.add(i);
}
linkedList
's elments should now read: 1,2,3,4,5,6,100,7,8,9,10
.
Edit: If you want to insert some value in an already existing LinkedList
:
linkedList.add((linkedList.indexOf(beforeTheNumber) >= 0 ? linkedList.indexOf(beforeTheNumber) : linkedList.size()), addNumber);
This code assumes that:
linkedList
exists and is aLinkedList<Integer>
.beforeTheNumber
andaddNumber
exist and are integers.
If linkedList
does not have beforeTheNumber
, addNumber
will be added to the end instead.
-
Thanks your answer. By this way we can traverse just once. But if there is a given linkedlist, we need another list to copy to Implement insertion。So there isn't a simple way to insert an element like typical C linked list in Linkedlist?xja– xja2019年04月28日 05:15:00 +00:00Commented Apr 28, 2019 at 5:15
-
The
java.util
package has a number of types similar toLinkedList
. Each one is used for different purposes and has a different set of methods. See the official Java tutorial for more information. For example, theArrayList
type has a methodadd(int i, E e)
that lets you add an elemente
at indexi
.ArrayList
also has a method calledindexOf(Object o)
which gets the index of the first occurrence ofo
.Cole Petersen– Cole Petersen2019年04月28日 05:36:14 +00:00Commented Apr 28, 2019 at 5:36 -
@Cole Petersen, it is exactly what author proposed by himself.Dzmitry Alifer– Dzmitry Alifer2019年04月28日 05:48:22 +00:00Commented Apr 28, 2019 at 5:48
-
@xja The answer has been updated with an example of how to do this with a
LinkedList
. Is this what you were looking for?Cole Petersen– Cole Petersen2019年04月28日 06:03:23 +00:00Commented Apr 28, 2019 at 6:03 -
@Cole This new answer will still traverse the linked list twice. The following Stephen C's answer perfectly solves this problem.xja– xja2019年04月28日 06:50:57 +00:00Commented Apr 28, 2019 at 6:50
LinkedList
.