反转单链表?
在不同的网站上,我正在寻找java中的程序来反转顺序 链表(单链表和双链表)。我登陆了不同的网站,例如
1)http://geek-o-pedia.blogspot.com/2007/07/how-would-you-reverse-singly -linked.html 2)http://stackoverflow.com/questions/354875/reversing-a-linked-list-in-java-recursively
PointA-根据我的理解,当您编写链接列表类时,这些程序(获取链接1)很好作为 程序假设我们可以访问我们不能访问的 Node 类(因为它是链表中的私有内部类。)
B 点 - 除此之外,该程序将永久反转源链表的顺序。所以当我们迭代这个 我们总是会以相反的顺序获得元素。
请让我知道上述两点是否正确
所以我尝试自己做
——反转单链表
LinkedList list1 = new LinkedList();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
LinkedList reverseList1 = new LinkedList();
int size= list1.size();
// below loop will revrse the order of source linked list i.e list1
for(int i =size-1;i>=0;i--)
{
reverseList1.add(size-i-1, list1.get(i));
}
只是想确保上述方法是否正确,因为我在互联网上找不到这些方法,我发现这些方法非常简单。无处不在我可以找到类似于
https://forums.oracle.com/forums/thread.jspa?threadID=2271413&tstart=0 也但没有得到正确的答案。
On different sites, i was looking for programme in java doing reversing the order
of linked list(Singly Linked List and and doubly linked list) .I landed up on different sites like
1)http://geek-o-pedia.blogspot.com/2007/07/how-would-you-reverse-singly-linked.html
2)http://stackoverflow.com/questions/354875/reversing-a-linked-list-in-java-recursively
PointA-As per my understanding these programmes(take link 1) are good when you are writing your linked list class as the
programme is assuming we can access Node class which we can not(As its is private inner class in Linked List.)
Point B-Apart from that this programme will permanently reverse the order of Source linked list. So when we iterate on this
we will always get the elemts in reverse order.
Please let me know if both the above points are correct
So i tried to do it myself
--Reversing the singly Linked List
LinkedList list1 = new LinkedList();
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
LinkedList reverseList1 = new LinkedList();
int size= list1.size();
// below loop will revrse the order of source linked list i.e list1
for(int i =size-1;i>=0;i--)
{
reverseList1.add(size-i-1, list1.get(i));
}
Just wanted to ensure if above approach is correct as i could not find these approach on internet which i found very simple .Everywhere i could find the approach similar to link1 and link2
posted at https://forums.oracle.com/forums/thread.jspa?threadID=2271413&tstart=0 too but did not get proper answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来效果很好。但是,
add
方法不需要第一个参数 - add 已插入到末尾(您也可以使用addLast
,这是相同的)。此外,多次使用
get(i)
效率不高。我将迭代第一个列表(使用 foreach 或迭代器 - ,并为每个元素调用addFirst
。或者,按照 panzerschreck 的建议,使用
Collections.reverse
,这实际上IMO 是最好的方法。This seems like it would work fine. However, there is no need for the 1st parameter for the
add
method - add already inserts at the end (and you can also useaddLast
, which is identical).Also, using
get(i)
that many times isn't efficient. I would iterate over the first list (with foreach or with an iterator - , and for each element calladdFirst
.Alternatively, use
Collections.reverse
, as panzerschreck suggested, which really is the best way IMO.您尝试过使用其中任何一个吗?
Did you try using any of these ?