java java结合两个链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6295277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java combine two linkedlist
提问by caesarkim
I have a question for combining two linkedlist. Basically, I want to append one linkedlist to the other linkedlist.
我有一个关于组合两个链表的问题。基本上,我想将一个链表附加到另一个链表。
Here is my solution. Is there a more efficient way to do it without looping the first linkedlist? Any suggestion would be appreciated.
这是我的解决方案。有没有更有效的方法来做到这一点而不循环第一个链表?任何建议将不胜感激。
static Node connect(LinkedList list1, LinkedList list2) {
Node original = list1.first;
Node previous = null;
Node current = list1.first;
while (current != null) {
previous = current;
current = current.next;
}
previous.next = list2.first;
return original;
}
回答by Kai
Use list1.addAll(list2)
to append list2 at the end of list1.
使用list1.addAll(list2)
追加列表2在列表1月底。
回答by Andrew Hill
For linked lists, linkedList.addAll(otherlist) seems to be a very poor choice.
对于链表,linkedList.addAll(otherlist) 似乎是一个非常糟糕的选择。
the java api version of linkedList.addAll begins:
linkedList.addAll 的 java api 版本开始:
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
so even when you have 2 linked lists, the second one gets converted to an array, then re-constituted into individual elements. This is worse than just merging 2 arrays.
因此,即使您有 2 个链表,第二个链表也会转换为数组,然后重新构成单个元素。这比合并 2 个数组更糟糕。
回答by Jeff Foster
I guess this is your own linked list implementation? With only a pointer to next element, the only way to append at the end is to walk all the elements of the first list.
我猜这是你自己的链表实现?只有指向下一个元素的指针,在末尾追加的唯一方法是遍历第一个列表的所有元素。
However, you could store a pointer to the last element to make this operation run in constant time (just remember to update the last element of the new list to be the last element of the added list).
但是,您可以存储指向最后一个元素的指针,以使此操作在恒定时间内运行(只需记住将新列表的最后一个元素更新为添加列表的最后一个元素)。
回答by Siddhartha Thota
The best way is to append the second list to the first list.
最好的方法是将第二个列表附加到第一个列表。
1. Create a Node Class.
1. 创建一个节点类。
2. Create New LinkedList Class.
2. 创建新的 LinkedList 类。
public class LinkedList<T> {
public Node<T> head = null;
public LinkedList() {}
public void addNode(T data){
if(head == null) {
head = new Node<T>(data);
} else {
Node<T> curr = head;
while(curr.getNext() != null) {
curr = curr.getNext();
}
curr.setNext(new Node<T>(data));
}
}
public void appendList(LinkedList<T> linkedList) {
if(linkedList.head == null) {
return;
} else {
Node<T> curr = linkedList.head;
while(curr != null) {
addNode((T) curr.getData());
curr = curr.getNext();
}
}
}
}
3. In the Main function or whereever you want this append to happen, do it like this.
3. 在 Main 函数中或任何您希望此追加发生的地方,请这样做。
LinkedList<Integer> n = new LinkedListNode().new LinkedList<Integer>();
n.addNode(23);
n.addNode(41);
LinkedList<Integer> n1 = new LinkedListNode().new LinkedList<Integer>();
n1.addNode(50);
n1.addNode(34);
n.appendList(n1);
I like doing this way so that there isn't any need for you to pass both these and loop again in the first LinkedList.
我喜欢这样做,这样您就不需要在第一个 LinkedList 中传递这些并再次循环。
Hope that helps
希望有帮助