LeetCode-2: Add Two Numbers

【Problem: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

【Solution】

1)From 九章算法:(can run, but "Submission Result:  Wrong Answer! ")

 1 class Solution:
 2     def addTwoNumbers(self, l1, l2):
 3         head = ListNode(0)
 4         ptr = head
 5         carry  = 0
 6         while True:
 7             if l1 != None:
 8                 carry += l1.val
 9                 l1 = l1.next
10             if l2 != None:
11                 carry += l2.val
12                 l2 = l2.next
13             ptr.val = carry % 10
14             carry /= 10
15             # 运算未结束新建一个节点用于储存答案,否则退出循环
16             if l1 != None or l2 != None or carry != 0:
17                 ptr.next = ListNode(0)
18                 ptr = ptr.next
19             else: 
20                 break
21         return head

 2)Java :

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
14         if(l1 == null && l2 == null) {
15             return null;
16         }
17             
18         ListNode head = new ListNode(0);
19         ListNode point = head;
20         int carry = 0;
21         while(l1 != null && l2!=null){
22             int sum = carry + l1.val + l2.val;
23             point.next = new ListNode(sum % 10);
24             carry = sum / 10;
25             l1 = l1.next;
26             l2 = l2.next;
27             point = point.next;
28         }
29         
30         while(l1 != null) {
31             int sum =  carry + l1.val;
32             point.next = new ListNode(sum % 10);
33             carry = sum /10;
34             l1 = l1.next;
35             point = point.next;
36         }
37         
38         while(l2 != null) {
39             int sum =  carry + l2.val;
40             point.next = new ListNode(sum % 10);
41             carry = sum /10;
42             l2 = l2.next;
43             point = point.next;
44         }
45         
46         if (carry != 0) {
47             point.next = new ListNode(carry);
48         }
49         return head.next;
50     }
51 }
52 
53 
54 // version: 高频题班
55 public class Solution {
56     /**
57      * @param l1: the first list
58      * @param l2: the second list
59      * @return: the sum list of l1 and l2
60      */
61     public ListNode addLists(ListNode l1, ListNode l2) {
62         // write your code here
63         ListNode dummy = new ListNode(0);
64         ListNode tail = dummy;
65 
66         int carry = 0;
67         for (ListNode i = l1, j = l2; i != null || j != null; ) {
68             int sum = carry;
69             sum += (i != null) ? i.val : 0;
70             sum += (j != null) ? j.val : 0;
71 
72             tail.next = new ListNode(sum % 10);
73             tail = tail.next;
74 
75             carry = sum / 10;
76             i = (i == null) ? i : i.next;
77             j = (j == null) ? j : j.next;
78         }
79 
80         if (carry != 0) {
81             tail.next = new ListNode(carry);
82         }
83         return dummy.next;
84     }
85 }

3)C++:yes, accepted

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        // 题意可以认为是实现高精度加法
        ListNode *head = new ListNode(0);
        ListNode *ptr = head;
        int carry = 0;
        while (true) {
            if (l1 != NULL) {
                carry += l1->val;
                l1 = l1->next;
            }
            if (l2 != NULL) {
                carry += l2->val;
                l2 = l2->next;
            }
            ptr->val = carry % 10;
            carry /= 10;
            // 当两个表非空或者仍有进位时需要继续运算,否则退出循环
            if (l1 != NULL || l2 != NULL || carry != 0) {
                ptr = (ptr->next = new ListNode(0));
            } else break;
        }
        return head;
    }
};

【References】

1、http://www.jiuzhang.com/solution/add-two-numbers/

Time complexity:O(n^2)2

原文地址:https://www.cnblogs.com/shenxiaolin/p/7924002.html