Add Two Numbers

Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807                                                                                                                                                          

思路:c++和java都可以使用类来实现链表

   数字相加的主要问题是处理大于9的结果,考虑到进位问题;

   还有就是 两个链表的长度不同的问题,综合起来就是数据结构中链表的连接,和多项式相加减比较类似;

   计算下一位的总和应该加上进位进行判断,是否仍有进位。

   主要是对于空链表的判断和不等长链表,以及一直进位的存在 比如  9999 +1 这种问题;

   基本思路没有问题,可以进行简化后,和答案属于同一个内容。

代码未进行简化:
class ListNode{
    int val;
    ListNode next;
    ListNode(int val){
        this.val = val;
    }
}
class Solution1 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //设置头节点
        int remain = 0,sum=0,num=0;//记录上一位的余数
        sum = l1.val+l2.val;
        num = sum%10;//计算
        remain = sum/10;//计算的十位数字
        ListNode list = new ListNode(num);
        ListNode head = list;
        l1 = l1.next;
        l2 = l2.next;
        while(l1!=null || l2!=null||remain!=0){
            if(l1==null&&l2==null){
                list.next = new ListNode(remain);
                list = list.next;
                remain = 0;
            }else if(l1==null&&l2!=null){
                while(l2!=null){
                    sum = l2.val+remain;
                    num = sum%10;
                    remain = sum/10;
                    list.next = new ListNode(num);
                    l2 = l2.next;
                    list = list.next;
                }
            }else if(l1!=null&&l2==null){
                while(l1!=null){
                    sum = l1.val+remain;
                    num = sum%10;
                    remain = sum/10;
                    list.next = new ListNode(num);
                    l1 = l1.next;
                    list = list.next;
                }
            }
            else{
                num = (l1.val+l2.val+remain)%10;//取得个位数字
                list.next = new ListNode(num);
                remain = (l1.val+l2.val+remain)/10;//计算进位
                l1 = l1.next;
                l2 = l2.next;
                list = list.next;
            }
        }
        return head;
    }
}
public class AddTwoNumbers {
    public static void main(String [] args){
        ListNode l1 = new ListNode(8);
        l1.next = new ListNode(9);
        l1.next.next = new ListNode(9);
        l1.next.next.next = null;
        ListNode l2 = new ListNode(2);
        l2.next = null;
//        l2.next.next = new ListNode(4);
//        l2.next.next.next = null;
        Solution1 s1 = new Solution1();
        ListNode list = s1.addTwoNumbers(l1,l2);
        while(list!=null){
            System.out.print(list.val+"\t");
            list = list.next;
        }

    }

  下面是leetcode答案给的代码:

ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;//简化了不等长链表 问题中存在进位问题 就是将不等长变为等长,多出去的节点位置的值 均为0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;//每次均将 进位加上
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;//简化了 对于不等长链表的判断
        if (q != null) q = q.next;
    }
    if (carry > 0) {//最后判断是否仍有进位  有的话就加上1进位 一般 为1
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}
路途遥远 仍需要多加练习呀;

  

 

posted @ 2018-07-02 12:27  Echonana  阅读(164)  评论(0编辑  收藏  举报