2016-11-12 8 views
0

リンクリスト全体がstutteredの場合はtrue、そうでない場合はfalseを返すコードを記述しています。突き刺さったリストは1,1,2,2,5,5,8,8で、突き返されないものは1,1,2,2,5,6,8,8のようなものになります。LinkList、double値のチェック

私はかなり長い間それを試してきましたが、正しい文を返すか、nullpointer例外を取得できないようです。

public boolean foo(){ 
    ListNode current = front; 
    ListNode runner = current.next; 
    while (current.next.next!=null){ //Looks two ahead for the end 
     if(current.data!=runner.data){  //They aren't equal, false 
      System.out.println(current.data); //just to see my data 
      System.out.println(runner.data); //debugging only 
      return false; 
     } 
     current = current.next.next; //increase by 2 
     runner = runner.next.next; // increase by 2 
     System.out.println(current.data + " ||" + runner.data); //again debugging 
    } 
    return true; // didn't register false, go ahead and true dat badboy. 
} 


    public static void main (String[] args){ 
    LinkedIntList list = new LinkedIntList(); 
    list.add(1); 
    list.add(1); 
    list.add(3); 
    list.add(3); 
    list.add(5); 
    list.add(5); 
    System.out.println(list.foo()); 
} 

誰かが明らかなエラーを見ていますか?私はcurrent.nextのwhileループを実行しようとしましたが、2回ではなく1回ずつランナーと電流を増加させましたが、どれもうまくいきませんでした。

+0

:すべての項目の以上は、そのアルゴリズムを少し変更ですか?あなたはそれを完璧にする必要がありますfooの上のメソッドを呼び出すことができますか? – baseballlover723

答えて

1

while (current.next.next!=null)の代わりに、while (runner.next!=null)をチェックしてください。また、最後の2つのノードのデータは、whileループの後に一致させる必要があります。

質問に記載されているようにリストに偶数の要素があると仮定すると、コードの次の変更によって正しい出力が得られます。

public boolean foo(){ 
    ListNode current = front; 
    ListNode runner = current.next; 
    while (runner.next!=null){ //Looks two ahead for the end 
     if(current.data!=runner.data){  //They aren't equal, false 
      System.out.println(current.data); //just to see my data 
      System.out.println(runner.data); //debugging only 
      return false; 
     } 
     current = current.next.next; //increase by 2 
     runner = runner.next.next; // increase by 2 
     System.out.println(current.data + " ||" + runner.data); //again debugging 
    } 
    if(current.data!=runner.data){  //They aren't equal, false 
     System.out.println(current.data); //just to see my data 
     System.out.println(runner.data); //debugging only 
     return false; 
    } 
    return true; // didn't register false, go ahead and true dat badboy. 
} 

良く&きれいな実装は以下のとおりである:それはcurrentcurrent.nextのどちらかがnullになることを完全に可能だから

public boolean foo(){ 
    ListNode current = front; 
    while (current != null){ 
     if(current.next == null) 
      return false; 
     if(current.data != current.next.data) 
      return false; 
     current = current.next.next; 
    } 
    return true; 
} 
+0

コードがfalseの場合でも、コードはtrueを返します。 5の1つを6に変更すると、返されます。 3 || 3 5 || 6 true。 – Podo

+0

あなたのコードブロックをまだチェックしていない場合は、それが動作するかどうかをお知らせします – Podo

+0

魅力的なように働いた! – Podo

3

あなたは盲目的に、最初のいくつかのチェックなしでcurrent.next.nextを使用することはできません。

この場合、nullポインタの問題が発生します。 「どもっ」場合は2を意味し、脇として

def isStuttered(node): 
    while node != null: 
    # Check if only one item left. 

    if node.next == null: 
     return false 

    # Check if not a pair. 

    if node.data != node.next.data: 
     return false 

    # Advance to next pair, okay as we have 2+ items left. 

    node = node.next.next 

    return true 

stuttered = isStuttered(head) 

:どもっ手段は任意の倍数ではなく、(あなたの例に従って)のみ倍増と仮定すると、

、それは以下のアルゴリズムでダウンし改善することができます`perfectStutter`方法が何であるかを

def isStuttered(node): 
    while node != null: 
    # Check if only one item left. 

    if node.next == null: 
     return false 

    # Check if not at least two. 

    val = node.data 
    if val != node.next.data: 
     return false 

    # Start with second item in set, 
    # advance to either new value or list end. 

    node = node.next 
    while node != null  # note 'and' must short-circuit 
    and node.data == val: 
     node = node.next 

    return true