2017-10-19 77 views
0

我無法將函數AddTwoNumber中的值返回給main。我已經在函數中檢查了結果,它是正確的。但是,當我將AddTwoNumber的值傳遞給ListNode start時,它不會打印任何內容。我認爲問題發生在這裏:無法打印出在Java中添加兩個數字

ListNode dummy = new ListNode(0); 
ListNode node = dummy; 

但我不知道如何解決它。

ListNode.cs:

class ListNode { 
    public int data;  // data stored in this node 
    public ListNode next; // link to next node in the list 

    // post: constructs a node with data 0 and null link 
    public ListNode() { 
     this(0, null); 
    } 

    // post: constructs a node with given data and null link 
    public ListNode(int data) { 
     this(data, null); 
    } 

    // post: constructs a node with given data and given link 
    public ListNode(int data, ListNode next) { 
     this.data = data; 
     this.next = next; 
    } 
} 

Main.cs:

public static void main(String[] args) { 

    ListNode first = new ListNode(8, 
        new ListNode(9, 
        new ListNode(7 ))); 

    ListNode second = new ListNode(3, 
         new ListNode(5, 
         new ListNode(6 ))); 

    ListNode start = AddTwoNumber(first,second); 

    while (start!=null) { 
     System.out.println(start.next); 
     start=start.next; 
    } 

} 

public static ListNode AddTwoNumber(ListNode first, ListNode second) { 
    ListNode dummy = new ListNode(0); 
    ListNode node = dummy; 
    int Digitsten = 0; 
    int sum = 0; 
    //Once fit first&second =null & Digitsten=0,the code can stop 
    while (first != null || second != null || Digitsten != 0) { 

     if (first != null && second != null) { 
      sum += first.data + second.data + Digitsten; 
     } else if (first!= null) { 
      sum += first.data + Digitsten; 
     } else if (second!= null) { 
      sum += second.data + Digitsten; 
     } else { 
      sum=Digitsten; `enter code here` 
     } 

     int DigitsOne = sum % 10; 

     Digitsten = sum/10; 

     node = new ListNode(DigitsOne); 
     node = node.next; 

     if (first == null) { 
      first = null; 
     } else { 
      first = first.next; 
     } 

     if (second == null) { 
      second = null; 
     } else { 
      second = second.next; 
     } 
     sum = 0; 

    } 
    return dummy.next; //return the value to dummy ListNode 
} 
+0

您的「添加方法」方法在哪裏?請發佈整個代碼。 –

+0

其實我已經把它貼在代碼中,對不起,我沒有把正確的名字,它的AddTwoNumber – yamahachou

回答

0

真正的問題是你AddTwoNumber方法內。 在移動到下一個節點之前,您無法使節點指向新節點,您應該使其下一個指向新節點。

public static ListNode AddTwoNumber(ListNode first, ListNode second){ 

ListNode dummy = new ListNode(0); 
ListNode node = dummy; 
int Digitsten = 0; 
int sum = 0; 

while (first != null || second != null || Digitsten != 0) 
{ 
    if (first != null && second != null) 
    { 
     sum += first.data + second.data + Digitsten; 
    } 
    else if (first!= null) 
    { 
     sum += first.data + Digitsten; 
    } 
    else if (second!= null) 
    { 
     sum += second.data + Digitsten; 
    } 
    else 
    { 
     sum=Digitsten; `enter code here` 
    } 

    int DigitsOne = sum % 10; 
    Digitsten = sum/10; 

    // LOOK HERE!!! 
    node.next = new ListNode(DigitsOne); 
    node = node.next; 


    if (first == null) 
    { 
     first = null; 
    } 
    else 
     first = first.next; 

    if (second == null) 
    { 
     second = null; 
    } 
    else 
    { 
     second = second.next; 
    } 
    sum=0; 
} 
return dummy.next;//return the value to dummy ListNode 
+0

非常感謝你!它的工作,你能告訴我問題在哪裏嗎?實際上,我對於指向新節點的概念以及下一個指向新節點的概念有點困惑 – yamahachou