【问题标题】:Counting all the nodes in a Linked List计算链表中的所有节点
【发布时间】:2014-02-28 04:23:02
【问题描述】:

我正在尝试编写一个简单的方法来计算链表中的所有节点。我知道链表中有 7 个项目,但它只返回其中的 6 个。

这是我的方法

public int count() {
    int count = 0;
    for (ListNode n = head; n.next != null; n = n.next) {
        count++;
    }
    return count;
}

这是我的 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;
}
}

【问题讨论】:

  • 案子解决了。 :D 请马上选择答案哈哈。

标签: java for-loop linked-list nodes


【解决方案1】:

结束节点将失败n.next != null 但它是 LinkedList 的一部分,因此您应该考虑这一点。听起来您只是有一个索引错误。

【讨论】:

    【解决方案2】:

    我想通了。

    for (ListNode n = head; n != null; n = n.next)
    

    n.next !=null 是错误。

    【讨论】:

    • 这是一种表达方式:D
    【解决方案3】:

    试试这个

    public int count() {
        int count = 0;
        for (ListNode n = head; n != null; n = n.next) {
            count++;
        }
        return count;
    }
    

    【讨论】:

      【解决方案4】:

      您想循环直到 n == null。就目前情况而言,你正在停止。

      【讨论】:

        【解决方案5】:

        那是因为您从 0 开始计数,忽略了第一个节点。

        改为初始化count=1

        【讨论】:

          【解决方案6】:
          public int count() {
              int count = 0;
              for (ListNode n = head; n != null; n = n.next) {
                  count++;
              }
              return count;
          }
          

          【讨论】:

            【解决方案7】:

            您没有计算最后一个节点。当您到达要计数的最后一个元素时,n.next 将为空,因此 count 永远不会增加。您可以改为尝试如下循环:

            ListNode n = head;
            for (ListNode n = head; n != null; n = n.next) {
              count++;
            }
            

            【讨论】:

              【解决方案8】:

              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.)
              

              【讨论】:

                【解决方案9】:

                您应该首先检查 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;
                

                【讨论】:

                  猜你喜欢
                  • 2022-11-03
                  • 2013-03-13
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-01-02
                  • 2017-03-13
                  • 1970-01-01
                  • 2022-01-05
                  相关资源
                  最近更新 更多