1. 程式人生 > >2. Add Two Numbers【medium】

2. Add Two Numbers【medium】

node cnblogs repr self lis clas null contain des

2. Add Two Numbers【medium】

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.

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

解法一:

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 4         ListNode * dummy = new ListNode(INT_MIN);
 5         ListNode * head = dummy;
 6         int carry = 0;
 7
8 while (l1 != NULL && l2 != NULL) { 9 int sum = l1->val + l2->val + carry; 10 carry = sum / 10; 11 head->next = new ListNode(sum % 10); 12 head = head->next; 13 l1 = l1->next; 14 l2 = l2->next;
15 } 16 17 while (l1 != NULL) { 18 int sum = l1->val + carry; 19 carry = sum / 10; 20 head->next = new ListNode(sum % 10); 21 head = head->next; 22 l1 = l1->next; 23 } 24 25 while (l2 != NULL) { 26 int sum = l2->val + carry; 27 carry = sum / 10; 28 head->next = new ListNode(sum % 10); 29 head = head->next; 30 l2 = l2->next; 31 } 32 33 if (carry) { 34 head->next = new ListNode(carry); 35 head = head->next; 36 } 37 38 head->next = NULL; 39 40 return dummy->next; 41 } 42 };

寫得太長了,下面有短碼的方法

解法二:

 1 public class Solution {
 2     /**
 3      * @param l1: the first list
 4      * @param l2: the second list
 5      * @return: the sum list of l1 and l2
 6      */
 7     public ListNode addLists(ListNode l1, ListNode l2) {
 8         // write your code here
 9         ListNode dummy = new ListNode(0);
10         ListNode tail = dummy;
11 
12         int carry = 0;
13         for (ListNode i = l1, j = l2; i != null || j != null; ) {
14             int sum = carry;
15             sum += (i != null) ? i.val : 0;
16             sum += (j != null) ? j.val : 0;
17 
18             tail.next = new ListNode(sum % 10);
19             tail = tail.next;
20 
21             carry = sum / 10;
22             i = (i == null) ? i : i.next;
23             j = (j == null) ? j : j.next;
24         }
25 
26         if (carry != 0) {
27             tail.next = new ListNode(carry);
28         }
29         return dummy.next;
30     }
31 }

參考了九章的代碼

解法三:

 1 ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
 2     ListNode preHead(0), *p = &preHead;
 3     int extra = 0;
 4     while (l1 || l2 || extra) {
 5         int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra;
 6         extra = sum / 10;
 7         p->next = new ListNode(sum % 10);
 8         p = p->next;
 9         l1 = l1 ? l1->next : l1;
10         l2 = l2 ? l2->next : l2;
11     }
12     return preHead.next;
13 }

參考了@ce2 的代碼

解法四:

 1 class Solution {
 2 public:
 3     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 4         // 題意可以認為是實現高精度加法
 5         ListNode *head = new ListNode(0);
 6         ListNode *ptr = head;
 7         int carry = 0;
 8         while (true) {
 9             if (l1 != NULL) {
10                 carry += l1->val;
11                 l1 = l1->next;
12             }
13             if (l2 != NULL) {
14                 carry += l2->val;
15                 l2 = l2->next;
16             }
17             ptr->val = carry % 10;
18             carry /= 10;
19             // 當兩個表非空或者仍有進位時需要繼續運算,否則退出循環
20             if (l1 != NULL || l2 != NULL || carry != 0) {
21                 ptr = (ptr->next = new ListNode(0));
22             } else break;
23         }
24         return head;
25     }
26 };

參考了九章的代碼

2. Add Two Numbers【medium】