package com.thinker.test.test2;

import java.util.Stack;

/**
 * @author lzh
 */
public class Test_2_5_2 {

    static class ListNode{
        public ListNode(){}
        public ListNode(int data){
            this.data = data;
        }
        ListNode next;
        int data;
        int sign;
    }

    public static ListNode go(ListNode a, ListNode b){
        if(a == null && b == null){
            return null;
        }
        int sum = 0;
        if(a != null){
            sum += a.data;
        }
        if(b != null){
            sum += b.data;
        }

        ListNode result = new ListNode();
        result.next = go(a==null?null:a.next,b==null?null:b.next);
        if(result.next != null){
            result.data = (sum%10+result.next.sign)%10;
            if(sum/10>=1 || (sum%10==9&&result.sign==1)){
                result.sign = 1;
            }
        }else {
            result.data = sum%10;
            if(sum/10>=1){
                result.sign = 1;
            }
        }
        return result;

    }

    public static void main(String[] args) {
        Stack stack = new Stack();
        ListNode a = new ListNode(6);
        a.next = new ListNode(1);
        a.next.next = new ListNode(7);
        ListNode b = new ListNode(2);
        b.next = new ListNode(9);
        b.next.next = new ListNode(5);
        ListNode result = go(a,b);
        System.out.println(result);
    }
}
