How else should I call the reverse function? The output shows only one link when reverse is performed on Rlist

26 Views Asked by At

Main function:

public static void main(String[] args) {

    AdditionLL Alist = new AdditionLL();
    AdditionLL Blist = new AdditionLL();
    AdditionLL Rlist = new AdditionLL();

    Alist.addNode(3); 
    Alist.addNode(7); 
    Alist.addNode(0); 
    Alist.addNode(4); 
    Alist.addNode(9); 

    Blist.addNode(5); 
    Blist.addNode(3); 
    Blist.addNode(2); 

    System.out.print("First list: ");
    Alist.printLL(Alist.head);
    Rlist.reverseLLtl(Rlist.head, null);                // this works
    System.out.println();
    System.out.print("Second list: ");
    Blist.printLL(Blist.head);
    System.out.println();

    Node Cnode = Rlist.addList(Alist.head, Blist.head, Rlist);
    Rlist.reverseLLtl(Rlist.head, null);              // THIS DOES NOT REVERSE THE ELEMENTS
    System.out.print("Resultant list: ");
    Rlist.printLL(Cnode);
}

addList function:

Node addList(Node A, Node B, AdditionLL C) {

    int res1 = 0;
    int res2 = 0;
    while(A != null) {
        res1 = (res1 + A.data)*10;
        A = A.next;
    }

    while(B != null) {
        res2 = (res2 + B.data)*10;
        B = B.next;
    }

    int result = res1 + res2;
    result = result/10;

    int data = 0;
    while(result > 0) {
        data = result % 10;
        C.addNode(data);
        result = (int)Math.floor(result/10);
    }

    return C.head;
}

}

How else should I call the reverse function? The output shows only one link when reverse is performed on Rlist. I don't understand how the function is being called differently in those two context. Rlist without reverse prints perfectly.

The code is written for:

Input: List1: 5->6->3 // represents number 365 List2: 8->4->2 // represents number 248 Output: Resultant list: 3->1->6 // represents number 613 Explanation: 365 + 248 = 613

0

There are 0 best solutions below