Java 计算链表中的所有节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22086128/
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
Counting all the nodes in a Linked List
提问by user2923395
I'm trying to write a simple method to count all the nodes in the linked list. I know there are 7 items in the linked list, but it is returning just 6 of them.
我正在尝试编写一个简单的方法来计算链表中的所有节点。我知道链表中有 7 个项目,但它只返回了其中的 6 个。
Here is my method
这是我的方法
public int count() {
int count = 0;
for (ListNode n = head; n.next != null; n = n.next) {
count++;
}
return count;
}
And here is my ListNode.java
这是我的 ListNode.java
public class ListNode {
String name; // a name in the list
ListNode next; // the next node in the list
ListNode prev; // the previous node in the list
/**
* Constructor with just a name. This form would be most useful when
* starting a list.
*/
public ListNode(String name) {
this.name = name;
next = null;
prev = null;
}
/**
* Constructor with a name and a reference to the previous list node. This
* form would be most useful when adding to the end of a list.
*/
public ListNode(String name, ListNode node) {
this.name = name;
next = null;
prev = node;
}
}
回答by Incognito
The end node will fail n.next != null
but it is part the the LinkedList, so you should consider that. It sounds like you simply have an indexing error.
结束节点将失败,n.next != null
但它是 LinkedList 的一部分,因此您应该考虑这一点。听起来你只是有一个索引错误。
回答by Tanuj Wadhwa
Well that's because you are starting your count from 0, neglecting the first node.
那是因为您从 0 开始计数,忽略了第一个节点。
Instead initialize count=1
;
而是初始化count=1
;
回答by evanchooly
You want to loop until n == null. As it stands, you're stopping one short.
你想循环直到 n == null。就目前而言,你正在停止一个短。
回答by stinepike
public int count() {
int count = 0;
for (ListNode n = head; n != null; n = n.next) {
count++;
}
return count;
}
回答by TheLostMind
n.next != null
Is your problem. Change it to n!=null
n.next != null
是你的问题。将其更改为n!=null
Example :
List : 1--> 2 -->3--> 4-->null
Count : 1--> 2-->3-->here n=4 and n.next=null. So, your loop will break and count will be 3 (i.e; the last node will not be counted.)
回答by Josh Liptzin
You aren't counting the last node. When you get to the last element to be counted, n.next will be null, and so count is never incremented. You might instead try a loop like the following:
您没有计算最后一个节点。当您到达要计算的最后一个元素时, n.next 将为空,因此 count 永远不会增加。您可以改为尝试如下循环:
ListNode n = head;
for (ListNode n = head; n != null; n = n.next) {
count++;
}
回答by user2923395
I figured it out.
我想到了。
for (ListNode n = head; n != null; n = n.next)
n.next !=null was the error.
n.next !=null 是错误。
回答by Mufanu
try this
尝试这个
public int count() {
int count = 0;
for (ListNode n = head; n != null; n = n.next) {
count++;
}
return count;
}
回答by Jesse Wijnakker
You should check for null first. If not 0, then set 'counter = 1' before you loop through it.
您应该先检查 null。如果不是 0,则在循环之前设置 'counter = 1'。
if (_first == null) return 0;
int count = 1;
for (ListNode n = _first; n.Next != null; n = n.Next)
{
count++;
}
return count;