Add Two Numbers

public class Solution {
    // http://www.cnblogs.com/springfor/p/3864493.html
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode h = new ListNode(-1);
        ListNode l3 = h;
        if(l1==null || l2==null) return h.next;
        int  carry=0;
        
        while(l1!=null||l2!=null){
            if(l1!=null){
                carry += l1.val;
                l1 = l1.next;
            }
            if(l2!=null){
                carry += l2.val;
                l2 = l2.next;
            }
            l3.next = new ListNode(carry%10);
            carry = carry/10;
            l3 = l3.next;
        }
        if(carry==1) l3.next = new ListNode(1); // do not forget
        return h.next;
    }
}

 我觉得题意很模糊,什么叫“reverse”

例子如下

Input: (2 -> 4 -> 3) + (4 -> 6 -> 4)
Output: 6 -> 0 -> 8

posted @ 2015-04-06 12:18  世界到处都是小星星  阅读(117)  评论(0编辑  收藏  举报