Add Two Numbers I & II

Add Two Numbers I

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse 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 7->1->6 + 5->9->2. That is, 617 + 295.

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

Given 3->1->5 and 5->9->2, return 8->0->8.

 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) return l2;
12         if (l2 == null) return l1;
13         
14         int value1 = 0, value2 = 0, carry = 0, sum = 0;
15         ListNode head = new ListNode(0);
16         ListNode current = head;
17         
18         while (l1 != null || l2 != null || carry == 1) {
19             value1 = (l1 == null ? 0 : l1.val);
20             value2 = (l2 == null ? 0 : l2.val);
21             sum = value1 + value2 + carry;
22             
23             current.next = new ListNode(sum % 10);
24             current = current.next;
25             
26             carry = sum / 10;
27             l1 = (l1 == null) ? null : l1.next;
28             l2 = (l2 == null) ? null : l2.next;
29         }
30         return head.next;
31     }
32 }

 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

 分析: reverse 之后再相加,然后再reverse result linkedlist

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

 方法二:用stack存list中的值,这样从上到下就是位数从小到大。

 1 public class Solution {
 2   public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 3     Stack<Integer> s1 = new Stack<>();
 4     Stack<Integer> s2 = new Stack<>();
 5 
 6     while (l1 != null) {
 7       s1.push(l1.val);
 8       l1 = l1.next;
 9     }
10 
11     while (l2 != null) {
12       s2.push(l2.val);
13       l2 = l2.next;
14     }
15     int carry = 0;
16     ListNode head = new ListNode(0);
17     while (!s1.empty() || !s2.empty() || carry != 0) {
18       int sum = 0;
19       if (!s1.empty()) {
20         sum += s1.pop();
21       }
22       if (!s2.empty()) {
23         sum += s2.pop();
24       }
25       sum += carry;
26       carry = sum / 10;
27       ListNode node = new ListNode(sum % 10);
28       node.next = head.next;
29       head.next = node;
30     }
31     return head.next;
32   }
33 }

 

posted @ 2016-07-16 00:11  北叶青藤  阅读(223)  评论(0编辑  收藏  举报