java中如何从链表中找到中间值或节点?
我的链表中有 50 个值。如何找到链表的中间值或节点?
List list = new LinkedList();
for (int i = 0; i < 50; i++) {
list.add(String.valueOf(i));
}
int size = list.size();
int middle = (size / 2);
System.out.println(list.get(middle).toString());...
我得到了这样的答案...... 但我的组长说要通过其他方式找? 是否有其他内置方法可以在链表中进行迭代?我尝试过...但我没有获得任何用于查找中间值的内置方法...并且 或者你可以建议另一种逻辑来查找链接列表中中间节点的值吗?
谢谢.......
I have the 50 values in linked list.how to find a middle value or node of linked list?
List list = new LinkedList();
for (int i = 0; i < 50; i++) {
list.add(String.valueOf(i));
}
int size = list.size();
int middle = (size / 2);
System.out.println(list.get(middle).toString());...
i got an answer like this....
But my team leader said to find in another way?
Is there any other built in method to iterate in linked list?i tried ...but i dint get any built in method for finding middle value...And
or can u any one suggest another logic to find the value of middle node in linke list?
thank you.......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获取对同一列表的 2 个引用。
Get 2 references to the same list.
也许您的团队领导建议您使用 ArrayList 而不是 LinkedList。
Maybe your team lead was suggesting you use an ArrayList rather than a LinkedList.