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

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
14         // Start typing your Java solution below
15         // DO NOT write main() function
16         ListNode answer = null;
17         ListNode next_node = null;
18         ListNode cur_node = null;
19         int carry = 0;
20         int sum   = 0;
21         boolean head = true;
22         ListNode l1_next = l1.next;
23         ListNode l1_cur  = l1;
24         ListNode l2_next = l2.next;
25         ListNode l2_cur  = l2;
26         while(l1_cur != null && l2_cur != null){
27             if((sum = l1_cur.val + l2_cur.val + carry) < 10){
28                 carry = 0;
29                 if(head == true){
30                     cur_node = new ListNode(sum);
31                     answer = cur_node;
32                     head = false;
33                 }
34                 else{
35                     ListNode new_node = new ListNode(sum);
36                     cur_node.next = new_node;
37                     cur_node = new_node;
38                 }
39             }
40             else{
41                 carry = 1;
42                 sum -= 10;
43                 if(head == true){
44                     cur_node = new ListNode(sum);
45                     answer = cur_node;
46                     head = false;
47                 }
48                 else{
49                     ListNode new_node = new ListNode(sum);
50                     cur_node.next = new_node;
51                     cur_node = new_node;
52                 }
53             }
54             l1_cur  = l1_cur.next;
55             l2_cur  = l2_cur.next;
56         }
57 
58         while(l1_cur != null){
59             if((sum = l1_cur.val + carry) < 10){
60                 carry = 0;
61                 ListNode new_node = new ListNode(sum);
62                 cur_node.next = new_node;
63                 cur_node = new_node;
64             }
65             else{
66                 carry = 1;
67                 sum -= 10;
68                 ListNode new_node = new ListNode(sum);
69                 cur_node.next = new_node;
70                 cur_node = new_node;
71             }
72             l1_cur  = l1_cur.next;
73         }
74         while(l2_cur != null){
75             if((sum = l2_cur.val + carry) < 10){
76                 carry = 0;
77                 ListNode new_node = new ListNode(sum);
78                 cur_node.next = new_node;
79                 cur_node = new_node;
80             }
81             else{
82                 carry = 1;
83                 sum -= 10;
84                 ListNode new_node = new ListNode(sum);
85                 cur_node.next = new_node;
86                 cur_node = new_node;
87             }
88             l2_cur  = l2_cur.next;
89         }
90         if(l1_cur == null && l2_cur == null && carry == 1){
91             ListNode new_node = new ListNode(carry);
92             cur_node.next = new_node;
93             cur_node = new_node;
94         }
95         return answer;
96     }
97 }

这一题还是挺简单的,就是有很多test case 需要考虑。。。 下次要争取一次考虑完全

 第二遍:

 1 public class Solution {
 2     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 3         // Start typing your Java solution below
 4         // DO NOT write main() function
 5         ListNode header = new ListNode(-1);
 6         ListNode cur = header;
 7         int carry = 0;
 8         while(l1 != null || l2 != null){
 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             ListNode tmp = new ListNode(carry % 10);
18             cur.next = tmp;
19             cur = cur.next;
20             carry /= 10;
21         }
22         if(carry != 0){
23             ListNode tmp = new ListNode(carry);
24             cur.next = tmp;
25         }
26         return header.next;
27     }
28 }

 

posted on 2013-03-27 06:39  Step-BY-Step  阅读(143)  评论(0编辑  收藏  举报

导航