简体   繁体   English

Java:如何在链表中添加(中间某处)多个元素?

[英]Java: How to add (somewhere in the middle) multiple elements in a linked list?

adding an element to a linked list is known to be O(1).将元素添加到链表已知是 O(1)。

However, adding it in position X is O(X) and if i want to add R elements in this position the total running time would be O(R*X).但是,将它添加到位置 X 是 O(X),如果我想在此位置添加 R 元素,则总运行时间将为 O(R*X)。

but there must be an O(X+R) solution.但必须有一个 O(X+R) 解决方案。

And the question is how to do the O(R+X) in java?问题是如何在 Java 中执行 O(R+X)?

You have a list of pairs (element, X) where X is an index and element is the item you want to put under that index.您有一个对(element, X)列表,其中X是一个索引, element是您要放在该索引下的项目。 Sort this list by X and add elements one after another using an Iterator .X对这个列表进行排序,并使用Iterator一个接一个地添加元素。 Here is an example:这是一个例子:

Your input is: [(E1, 5), (E2, 3), (E3, 7)] .您的输入是: [(E1, 5), (E2, 3), (E3, 7)]

  1. Sort it by index: [(E2, 3), (E1, 5), (E3, 7)]按索引排序: [(E2, 3), (E1, 5), (E3, 7)]

  2. Create an iterator and advance it by 3 .创建一个迭代器并将其前进3

  3. Add E2 using an iterator.使用迭代器添加E2

  4. Advance the same iterator by 2 ( 5 - 3 ).将同一个迭代器前进2 ( 5 - 3 )。

  5. Add E1 .添加E1

  6. ... ...

Notice that this algorithm has an of-by-one bug.请注意,此算法有一个一对一的错误。 It should be relatively easy to fix it.修复它应该相对容易。

UPDATE: just noticed that your problem is much simpler.更新:刚刚注意到您的问题要简单得多。 In your case just create an iterator, advance it X times and add elements one by one using that iterator.在您的情况下,只需创建一个迭代器,将其前进X次并使用该迭代器一个一个地添加元素。

Assuming you're using java.util.LinkedList , there is the LinkedList.listIterator () method that returns a ListIterator:假设您使用的是java.util.LinkedList ,则存在返回 ListIterator 的LinkedList.listIterator () 方法:

public ListIterator listIterator(int index)公共 ListIterator listIterator(int index)

Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.返回此列表中元素的列表迭代器(以适当的顺序),从列表中的指定位置开始。 [...] [...]

The list-iterator is fail-fast: if the list is structurally modified at any time after the Iterator is created, in any way except through the list-iterator's own remove or add methods, the list-iterator will throw a ConcurrentModificationException.列表迭代器是快速失败的:如果列表在创建迭代器后的任何时候在结构上被修改,除了通过列表迭代器自己的删除或添加方法之外,列表迭代器将抛出一个 ConcurrentModificationException。 [...] [...]

And you can use ListIterator.add () to safely add an item in the middle of the LinkedList:并且您可以使用ListIterator.add () 安全地在 LinkedList 的中间添加一个项目:

void add(E e)无效添加(E e)

Inserts the specified element into the list (optional operation).将指定元素插入列表(可选操作)。 [...] [...]

For example, say you want to put list2 in the 5th position of the list1, you can do so by doing the following:例如,假设你想将 list2 放在 list1 的第 5 个位置,你可以通过执行以下操作来实现:

LinkedList list1 = ...;
LinkedList list2 = ...;

ListIterator it1 = list1.listIterator(5);
for (Object item : list2) {
    it1.add(item);
}

Put elements to collection and then you can use addAll method to add them all.将元素放入集合中,然后您可以使用addAll方法将它们全部添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM