LeetCode 2. Add Two Numbers

原题链接:https://leetcode.com/problems/add-two-numbers/

题目:

You are given two linked lists representing two non-negative numbers. 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.

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

题解:

思路两个val和进位carry相加,组成新点往后连,注意两个list长度不同和最后是否还有一个进位的情况。

Time Complexity: O(n), n是较长list的长度.

Space: O(n).

AC Java:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11         if(l1 == null){
12             return l2;
13         }
14         if(l2 == null){
15             return l1;
16         }
17         
18         ListNode dummy = new ListNode(0);
19         ListNode cur = dummy;
20         int carry = 0;
21         
22         while(l1 != null || l2 != null){
23             if(l1 != null){
24                 carry += l1.val;
25                 l1 = l1.next;
26             }
27             if(l2 != null){
28                 carry += l2.val;
29                 l2 = l2.next;
30             }
31             
32             cur.next = new ListNode(carry%10);
33             cur = cur.next;
34             carry = carry/10;
35         }
36         if(carry != 0){
37             cur.next = new ListNode(carry);
38         }
39         return dummy.next;
40     }
41 }

AC C++:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14         if(!l1){
15             return l2;
16         }
17 
18         if(!l2){
19             return l1;
20         }
21 
22         ListNode* dummy = new ListNode(-1);
23         ListNode* cur = dummy;
24         int carry = 0;
25         while(l1 || l2){
26             if(l1){
27                 carry += l1->val;
28                 l1 = l1->next;
29             }
30 
31             if(l2){
32                 carry += l2->val;
33                 l2 = l2->next;
34             }
35 
36             cur->next = new ListNode(carry % 10);
37             cur = cur->next;
38             carry /= 10;
39         }
40 
41         if(carry != 0){
42             cur->next = new ListNode(carry);
43         }
44 
45         return dummy->next;
46     }
47 };

也可以选择更改较长list的node val.

不需要n的extra space.

Time Complexity: O(n). 

Space: O(1).

AC Java:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11         if(l1 == null){
12             return l2;
13         }
14         if(l2 == null){
15             return l1;
16         }
17         int len1 = 0;
18         ListNode cur = l1;
19         while(cur != null){
20             cur = cur.next;
21             len1++;
22         }
23         
24         int len2 = 0;
25         cur = l2;
26         while(cur != null){
27             cur = cur.next;
28             len2++;
29         }
30         
31         ListNode dummy = new ListNode(0);
32         if(len1 > len2){
33             dummy.next = l1;
34         }else{
35             dummy.next = l2;
36         }
37         cur = dummy;
38         int carry = 0;
39         
40         while(l1 != null || l2 != null){
41             if(l1 != null){
42                 carry += l1.val;
43                 l1 = l1.next;
44             }
45             if(l2 != null){
46                 carry += l2.val;
47                 l2 = l2.next;
48             }
49             cur.next.val = carry%10;
50             cur = cur.next;
51             carry /= 10;
52         }
53         if(carry != 0){
54             cur.next = new ListNode(1);
55         }
56         return dummy.next;
57     }
58 }

跟上Add Two Numbers IISum of Two Integers.

posted @ 2015-08-29 05:39  Dylan_Java_NYC  阅读(260)  评论(0编辑  收藏  举报