2. Add Two Numbers

直接用一个carry记录进位就可以

 

 

 

 

 1 //NEW
 2 class Solution {
 3     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 4         ListNode root = new ListNode(0);
 5         return addTwoNumbers(l1, l2, root);
 6     }
 7     public ListNode addTwoNumbers(ListNode l1, ListNode l2, ListNode root){
 8         int carry = 0;
 9         ListNode dummy = root;
10         while(l1 != null || l2!=null){
11             int x = (l1 != null) ? l1.val : 0;
12             int y = (l2 != null) ? l2.val : 0;
13             dummy.next = new ListNode((carry + x + y)%10);
14             dummy = dummy.next;
15             carry = (carry + x + y)/10;
16             if(l1!=null) {
17                 l1 = l1.next;
18             }
19             if(l2!=null) {
20                 l2 = l2.next;
21             }
22         }
23         if (carry > 0) {
24         dummy.next = new ListNode(carry);
25         }
26         return root.next;
27     }
28 }
29 
30 
31 //Old
32 class Solution {
33     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
34         String str1 = "", str2 = "";
35         while(l1 != null) {
36             str1 += Integer.toString(l1.val);
37             l1 = l1.next;
38         }
39         while(l2 != null) {
40             str2 += Integer.toString(l2.val);
41             l2 = l2.next;
42         }
43         char[] str11 = str1.toCharArray();
44         char[] str21 = str2.toCharArray();
45         char[] res = new char[Math.max(str11.length, str21.length) + 1];
46         for(int i = 0; i < res.length; i++) {
47             res[i] = 'a';
48         }
49         res[res.length - 1] = '0';
50         int record = 0;
51         for(int i = 0; i < res.length; i++) {
52             if(i < str11.length && i < str21.length) {
53                 if(str11[i]-'0' + str21[i]-'0' + record >= 10) {
54                     res[i] = (char)('0'+ ((str11[i]-'0') + (str21[i]-'0') +record) % 10);
55                     record = 1;
56                 }else {
57                     res[i] = (char)('0'+ (str11[i]-'0' + str21[i]-'0' +record));
58                     record = 0;
59                 }
60                 
61             }else if(i < str21.length) {
62                 if(str21[i]-'0' +record >= 10) {
63                     res[i] = (char)('0'+ (str21[i]-'0' +record) % 10);
64                     record = 1;
65                 }else {
66                     res[i] = (char)('0'+ (str21[i]-'0' +record));
67                     record = 0;
68                 }
69             }else if(i < str11.length){
70                 if(str11[i]-'0' +record >= 10) {
71                     res[i] = (char)('0'+ (str11[i]-'0' +record) % 10);
72                     record = 1;
73                 }else {
74                     res[i] = (char)('0'+ (str11[i]-'0' +record));
75                     record = 0;
76                 }
77             }else {
78                 if(record == 1) {
79                     res[i] = '1';
80                 }
81             }
82         }
83         ListNode node = new ListNode(res[0]-'0');
84         ListNode head = node;
85         for(int i = 1; i < res.length; i++) {
86             if(i != res.length - 1 || res[i] == '1') {
87                 node.next = new ListNode(res[i]-'0');
88                 node = node.next;
89             }    
90                 
91         }
92         return head;
93     }
94 }

 

posted @ 2018-09-04 09:11  jasoncool1  阅读(120)  评论(0编辑  收藏  举报