leetcode 2. 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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

小结:链表还不是很熟悉,耗时: code:30min debug 20min

// 我的解法 O(n) 
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1 == null || l2 == null){
            return l1 == null ? l2 : l1;
        }
        ListNode resHead = new ListNode(-1);  // 定义头结点
        ListNode point = new ListNode(-1);  // 定义指针结点
        resHead.next = point;
        int lastfalg = 0;
        while (l1 != null && l2 != null) {
            ListNode node = new ListNode(-1); //  注意: 最好是每次新建node,然后赋值给point指针
            int tmp = l1.val + l2.val + lastfalg;
            int flag = tmp >= 10 ? 1 : 0;
            lastfalg = flag;
            if (flag == 1) {
                node.val = tmp -10;
            } else {
                node.val = tmp;
            }
            point.next = node; // 结点赋值
            point = point.next;  // 指针指向当前结点下一个结点
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null) {
            ListNode node = new ListNode(-1);
            int tmp = l1.val + lastfalg;
            if (lastfalg == 1) {
                lastfalg = 0;
            }
            int flag = tmp >= 10 ? 1 : 0;
            lastfalg = flag;
            if (flag == 1) {
                node.val = tmp -10;
            } else {
                node.val = tmp;
            }
            l1 = l1.next;
            point.next = node;
            point = point.next;
        }
        while (l2 != null) {
            ListNode node = new ListNode(-1);
            int tmp = l2.val + lastfalg;
            if (lastfalg == 1) {
                lastfalg = 0;
            }
            int flag = tmp >= 10 ? 1 : 0;
            lastfalg = flag;
            if (flag == 1) {
                node.val = tmp -10;
            } else {
                node.val = tmp;
            }
            l2 = l2.next;
            point.next = node;
            point = point.next;
        }
        if (lastfalg == 1) {  
            point.next = new ListNode(lastfalg);  // 半天还是有一种情形忽略,就是最后一位进位可能没有处理的情形
        }
        return resHead.next.next;
    }
    
   public static void main(String[] args) {
        List array1 = Lists.newArrayList(5);
        List array2 = Lists.newArrayList(5);
        while (true){
            ListNode l1 = made(array1);
            ListNode l2 = made(array2);
            ListNode res = addTwoNumbers(l1, l2);
            System.out.println(res);
        }
    }
   // 中间构造链表的函数还写了一会儿!
    private static ListNode made (List list) {
        ListNode head = new ListNode(-1);
        ListNode p = new ListNode(-1);
        head.next = p;
        for (Integer a : list) {
            ListNode tmp  = new ListNode(a);
            p.next = tmp;
            p = p.next;
        }
        return head.next.next;
    }
// good cooder 也是O(n)的,但是显然更简洁
    public static ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode p = l1, q = l2, curr = head; // 这里确实应该用指针,而不是传入参数,否则有可能破坏对象
        int carry = 0;
        while (p != null || q != null) { // 这里的简洁是用||将过程统一
            int x = p == null ? 0 : p.val;
            int y = q == null ? 0 : q.val;
            int sum = x + y + carry;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next; // 保证curr每次都指向最近更新的结点
            if(p != null ) {
                p = p.next;
            }
            if (q != null) {
                q =q.next;
            }
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return curr.next;
    }

后记:确实得多看,多写,多读别人的代码才能提高,否则只看自己写的,不可能提高。编程无捷径!

你可能感兴趣的:(leetcode 2. Add Two Numbers)