【问题标题】:Java Splitting integer Linked ListJava拆分整数链表
【发布时间】:2012-05-03 06:09:43
【问题描述】:

我需要编写一个以单个整数链表和一个称为拆分值的特殊值开头的方法。列表的元素没有特定的顺序。该方法将节点划分为两个链表:一个包含包含小于拆分值的元素的所有节点,另一个包含所有其他节点。如果原始链表有任何重复的整数(即任何两个或多个具有相同元素的节点),则具有该元素的新链表应该具有相同数量的重复该元素的节点。该方法返回两个头引用 - 每个已创建的链表一个。

我已经花费了无数个小时试图做到这一点,并认为这是最接近的,但我在编译时出错,我的 copyTail* IntNodes 可能未初始化。我的代码也可能完全错误.... 任何帮助指出我正确的方向??

public static IntNode[ ] listSplitLessGreater(IntNode source, int splitter)
{
  IntNode copyHeadLess;
  IntNode copyTailLess;
  IntNode copyHeadGreater;
  IntNode copyTailGreater;
  IntNode[ ] answer = new IntNode[2];
    boolean less = true;
    boolean greater = true;

  // Handle the special case of the empty list.   
  if (source == null)
     return answer; // The answer has two null references .

  //Split list into two lists less and greater/equal than splitter.     
  while (source.link != null)
  {
    if (splitter < source.data)
        {
            if (less)
            {
            copyHeadLess = new IntNode(source.data, null);
            copyTailLess = copyHeadLess;
            less=false;
            }
            else
            {
            source = source.link;
            copyTailLess.addNodeAfter(source.data);
            copyTailLess = copyTailLess.link;

            }
        }
        else
        {
            if (greater)
            {
            copyHeadGreater = new IntNode(source.data, null);
            copyTailGreater = copyHeadGreater;
            greater=false;
            }
            else
            {
            source = source.link;
            copyTailGreater.addNodeAfter(source.data);
            copyTailGreater = copyTailGreater.link;

            }
        }

    }      

  //Return Head References
  answer[0] = copyHeadLess;
  answer[1] = copyHeadGreater;

  return answer;

}

【问题讨论】:

    标签: java linked-list


    【解决方案1】:

    我认为您通过仅使用单个类 (IntNode) 对列表进行建模,使其变得比需要的更复杂。如果将其建模为“列表”和“列表中的节点”,那么很容易得到一个 empty 列表。您也不需要同时跟踪头部和尾部 - 列表可以做到这一点。到那时,就很简单了:

    • 创建两个空列表,一个用于“较低”,一个用于“不较低”
    • 遍历原始列表:
      • 确定要将元素添加到哪个列表
      • 添加元素
    • 返回两个列表(例如,像您一样使用数组)

    请注意,即使没有这个,您也可以通过使用null 来表示“我还没有这个列表”来简化您的代码。目前您的代码无法编译,因为copyHeadLess 等在使用时没有明确分配知道在分配它们之前不会尝试使用它们,但编译器不会。不过,我仍然推荐改造方法:)

    【讨论】:

      【解决方案2】:

      如果 source 不为空,但 source.link 为空(列表仅由一个元素组成),那么您永远不会分配给您的 copyHeadLess 等变量。尝试将它们初始化为 null 或任何默认值:

        IntNode copyHeadLess = null;
        IntNode copyTailLess = null; 
        IntNode copyHeadGreater = null; 
        IntNode copyTailGreater = null; 
        IntNode[ ] answer = new IntNode[2]; 
          boolean less = true; 
          boolean greater = true; 
      
        // Handle the special case of the empty list.    
        if (source == null) 
           return answer; // The answer has two null references . 
      
        //Split list into two lists less and greater/equal than splitter.      
        while (source.link != null) 
        { 
          // what about case where source isn't null but source.link is null?
        }       
      
        //Return Head References 
        answer[0] = copyHeadLess; // this may have never been assigned in your original code
        answer[1] = copyHeadGreater; // this may have never been assigned in your original code
      
        return answer; 
      
       }
      

      【讨论】:

        猜你喜欢
        • 2010-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-08
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        相关资源
        最近更新 更多