LeetCode 445. Add Two Numbers II

原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/

题目:

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

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

题解:

类似Add Two Numbers.

可看成是Add Two NumbersReverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.

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 class Solution {
10     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11         l1 = reverse(l1);
12         l2 = reverse(l2);
13         
14         ListNode dummy = new ListNode(0);
15         ListNode cur = dummy;
16         int carry = 0;
17         
18         while(l1 != null || l2!= null){
19             if(l1 != null){
20                 carry += l1.val;
21                 l1 = l1.next;
22             }
23             
24             if(l2 != null){
25                 carry += l2.val;
26                 l2 = l2.next;
27             }
28             
29             cur.next = new ListNode(carry%10);
30             carry /= 10;
31             cur = cur.next;
32         }
33         
34         if(carry != 0){
35             cur.next = new ListNode(carry);
36         }
37         
38         ListNode head = dummy.next;
39         dummy.next = null;
40         return reverse(head);
41     }
42     
43     private ListNode reverse(ListNode head){
44         if(head == null || head.next == null){
45             return head;
46         }
47         
48         ListNode tail = head;
49         ListNode cur = head;
50         ListNode pre;
51         ListNode temp;
52         while(tail.next != null){
53             pre = cur;
54             cur = tail.next;
55             temp = cur.next;
56             cur.next = pre;
57             tail.next = temp;
58         }
59         
60         return cur;
61     }
62 }

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         l1 = reverse(l1);
23         l2 = reverse(l2);
24 
25         ListNode* dummy = new ListNode(-1);
26         ListNode* cur = dummy;
27         int carry = 0;
28         while(l1 || l2){
29             if(l1){
30                 carry += l1->val;
31                 l1 = l1->next;
32             }
33 
34             if(l2){
35                 carry += l2->val;
36                 l2 = l2->next;
37             }
38 
39             cur->next = new ListNode(carry % 10);
40             carry /= 10;
41             cur = cur->next;
42         }
43 
44         if(carry > 0){
45             cur->next = new ListNode(carry);
46         }
47         
48         ListNode* head = dummy->next;
49         dummy->next = nullptr;
50         return reverse(head);
51     }
52 
53     ListNode* reverse(ListNode* head){
54         if(!head || !head->next){
55             return head;
56         }
57 
58         ListNode* tail = head;
59         ListNode* cur = head;
60         ListNode* pre;
61         ListNode* temp;
62         while(tail->next){
63             pre = cur;
64             cur = tail->next;
65             temp = cur->next;
66             cur->next = pre;
67             tail->next = temp;
68         }
69 
70         return cur;
71     }
72 };

也可以使用两个stack把list 1 和 list 2 分别压进去. 再pop出来相加放到new list的head位置.

Time Complexity: O(n). 压stack用了O(n), pop后相加用了O(n).

Space: O(n). stack用了O(n). result list用了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         Stack<Integer> stk1 = new Stack<Integer>();
19         Stack<Integer> stk2 = new Stack<Integer>();
20         while(l1 != null){
21             stk1.push(l1.val);
22             l1 = l1.next;
23         }
24         while(l2 != null){
25             stk2.push(l2.val);
26             l2 = l2.next;
27         }
28         
29         ListNode dummy = new ListNode(0);
30         int carry = 0;
31         while(!stk1.isEmpty() || !stk2.isEmpty()){
32             if(!stk1.isEmpty()){
33                 carry += stk1.pop();
34             }
35             if(!stk2.isEmpty()){
36                 carry += stk2.pop();
37             }
38             ListNode cur = new ListNode(carry%10);
39             cur.next = dummy.next;
40             dummy.next = cur;
41             carry /= 10;
42         }
43         if(carry != 0){
44             ListNode cur = new ListNode(1);
45             cur.next = dummy.next;
46             dummy.next = cur;
47         }
48         return dummy.next;
49     }
50 }

 

posted @ 2016-12-26 09:03  Dylan_Java_NYC  阅读(1103)  评论(0编辑  收藏  举报