无法打印出来用Java添加两个数字

时间:2017-10-19 03:13:01

标签: java algorithm

我无法将函数AddTwoNumber中的值返回到main。我已经检查了函数中的结果,这是正确的。但是,当我将AddTwoNumber中的值传递给ListNode start时,它不会打印任何内容。我认为这个问题正在发生:

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
}

1 个答案:

答案 0 :(得分: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
相关问题