221. Add Two Numbers II【medium】

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in forward order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

 
Example

Given 6->1->7 + 2->9->5. That is, 617 + 295.

Return 9->1->2. That is, 912.

 

解法一:

 1 class Solution {
 2 public:
 3     /**
 4      * @param l1: the first list
 5      * @param l2: the second list
 6      * @return: the sum list of l1 and l2 
 7      */
 8     ListNode *addLists2(ListNode *l1, ListNode *l2) {
 9         reverse(l1);
10         reverse(l2);
11         ListNode *dummy = new ListNode(0);
12         ListNode *tail = dummy;
13         int carry = 0;
14 
15         while (l1 != NULL || l2 != NULL) {
16             int sum = carry;
17             if (l1 != NULL) {
18                 sum += l1->val;
19                 l1 = l1->next;
20             }
21             if (l2 != NULL) {
22                 sum += l2->val;
23                 l2 = l2->next;
24             }
25             if (sum > 9) {
26                 carry = 1;
27                 sum -= 10; 
28             } else {
29                 carry = 0;
30             }
31             tail->next = new ListNode(sum);
32             tail = tail->next;
33         }
34 
35         if (carry == 1) {
36             tail->next = new ListNode(1);
37             tail = tail->next;
38         }
39 
40         reverse(dummy->next);
41 
42         return dummy->next;
43     }
44 
45 
46     void reverse(ListNode *&head) {
47         ListNode *prev = NULL;
48 
49         while (head != NULL) {
50             ListNode *temp = head->next;
51             head->next = prev;
52             prev = head;
53             head = temp;
54         }
55 
56         head = prev;
57     }
58 };

链表先翻转,再求和,然后再翻转

 

解法二:

 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 addLists2(ListNode l1, ListNode l2) {  
 8         Stack<Integer> temp1 = reverseNode(l1);  
 9         Stack<Integer> temp2 = reverseNode(l2);  
10 
11         ListNode point = new ListNode(0);  
12         int flag = 0;  
13 
14         while((!temp1.isEmpty()) && (!temp2.isEmpty())){  
15             int value = temp1.pop() + temp2.pop() + flag;  
16             flag = value / 10;  
17             value = value % 10;  
18             ListNode cur = new ListNode(value);  
19             cur.next = point.next;  
20             point.next = cur;  
21         }  
22 
23         while(!temp1.isEmpty()){  
24             int value = temp1.pop() + flag;  
25             flag = value / 10;  
26             value = value % 10;  
27             ListNode cur = new ListNode(value);  
28             cur.next = point.next;  
29             point.next = cur;  
30         }  
31 
32         while(!temp2.isEmpty()){  
33             int value = temp2.pop() + flag;  
34             flag = value / 10;  
35             value = value % 10;  
36             ListNode cur = new ListNode(value);  
37             cur.next = point.next;  
38             point.next = cur;  
39         }  
40 
41         if(flag == 1){  
42             ListNode cur = new ListNode(1);  
43             cur.next = point.next;  
44             point.next = cur;  
45         }  
46 
47         return point.next;  
48     }  
49      
50  
51     public Stack<Integer> reverseNode(ListNode temp){  
52         Stack<Integer> record = new Stack<Integer>();  
53 
54         while(temp != null){  
55             record.push(temp.val);  
56             temp = temp.next;  
57         }  
58 
59         return record;  
60     }  
61 }  

利用栈的先进后出,记录两个链表的节点值。然后计算对应位置的两个数字之和,如果有进位,赋值给flag并带入下一个计算。

参考@sunday0904 的代码

 

转载于:https://www.cnblogs.com/abc-begin/p/8151240.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值