Add Two Numbers I
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
思路
- 从同时从2个linkedlist头部开始遍历,算2个数的和。
- 怎么算?
sum = (l1.val + l2.val + carry) % 10
,carry = (l1.val + l2.val) / 10
- 不要忘了,l1和l2的长度可能不等,所以当某一个已经遍历完以后,还需要把剩下的那个遍历完。
-
最后不要忘了最后一个可能出现的carry。比如
999 + 99 => 9->8->0->(1)
但是,如果carry是0时就不需要加了。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
}
int carry = 0;
ListNode dummy = new ListNode(0);
ListNode head = dummy;
// Queue<ListNode> queue = new LinkedList<ListNode>();
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + carry;
int cur = sum % 10;
carry = sum / 10;
head.next = new ListNode(cur);
head = head.next;
l1 = l1.next;
l2 = l2.next;
}
while (l1 != null) {
int sum = l1.val + carry;
int cur = sum % 10;
carry = sum / 10;
head.next = new ListNode(cur);
head = head.next;
l1 = l1.next;
}
while (l2 != null) {
int sum = l2.val + carry;
int cur = sum % 10;
carry = sum / 10;
head.next = new ListNode(cur);
head = head.next;
l2 = l2.next;
}
if (carry != 0) {
head.next = new ListNode(carry);
}
return dummy.next;
}
}
Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. 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
思路
- 不能翻转listNode的情况下,就只能借助外部结构来实现翻转linkedList了,那么可以用FILO的STACK来分别存储L1和L2
- 同时从q1/q2中取出节点,算2者相加的结果(逻辑与Add Two Numbers一致),算好的结果仍然放入到第三个STACK结构中
- 从存结果的stack中依次取出结果的每个节点,并组织成LinkedList。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//1. 不能翻转listNode的情况下,就只能借助外部结构来实现翻转linkedList了,那么久可以用FILO的STACK来存储L1和L2
Stack<ListNode> q1 = new Stack<ListNode>();
Stack<ListNode> q2 = new Stack<ListNode>();
while (l1 != null) {
q1.add(l1);
l1 = l1.next;
}
while (l2 != null) {
q2.add(l2);
l2 = l2.next;
}
//2. 同时从q1/q2中取出节点,算2者相加的结果(逻辑与Add Two Numbers一致),算好的结果仍然放入到STACK结构中
Stack<ListNode> result = new Stack<ListNode>();
int carry = 0;
while (!q1.isEmpty() && !q2.isEmpty()) {
int sum = q1.pop().val + q2.pop().val + carry;
int cur = sum % 10;
carry = sum / 10;
result.add(new ListNode(cur));
}
while (!q1.isEmpty()) {
int sum = q1.pop().val + carry;
int cur = sum % 10;
carry = sum / 10;
result.add(new ListNode(cur));
}
while (!q2.isEmpty()) {
int sum = q2.pop().val + carry;
int cur = sum % 10;
carry = sum / 10;
result.add(new ListNode(cur));
}
if (carry != 0) {
result.add(new ListNode(carry));
}
//3. 从stack中依次取出结果的每个节点,并组织成LinkedList
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while (!result.isEmpty()) {
head.next = result.pop();
head = head.next;
}
return dummy.next;
}
}