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
思路
- 從同時(shí)從2個(gè)linkedlist頭部開始遍歷餐屎,算2個(gè)數(shù)的和徐块。
- 怎么算此洲?
sum = (l1.val + l2.val + carry) % 10
,carry = (l1.val + l2.val) / 10
- 不要忘了畅厢,l1和l2的長(zhǎng)度可能不等番宁,所以當(dāng)某一個(gè)已經(jīng)遍歷完以后元莫,還需要把剩下的那個(gè)遍歷完。
-
最后不要忘了最后一個(gè)可能出現(xiàn)的carry蝶押。比如
999 + 99 => 9->8->0->(1)
但是踱蠢,如果carry是0時(shí)就不需要加了。
/**
* 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
思路
- 不能翻轉(zhuǎn)listNode的情況下棋电,就只能借助外部結(jié)構(gòu)來(lái)實(shí)現(xiàn)翻轉(zhuǎn)linkedList了茎截,那么可以用FILO的STACK來(lái)分別存儲(chǔ)L1和L2
- 同時(shí)從q1/q2中取出節(jié)點(diǎn),算2者相加的結(jié)果(邏輯與Add Two Numbers一致)赶盔,算好的結(jié)果仍然放入到第三個(gè)STACK結(jié)構(gòu)中
- 從存結(jié)果的stack中依次取出結(jié)果的每個(gè)節(jié)點(diǎn)企锌,并組織成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. 不能翻轉(zhuǎn)listNode的情況下于未,就只能借助外部結(jié)構(gòu)來(lái)實(shí)現(xiàn)翻轉(zhuǎn)linkedList了撕攒,那么久可以用FILO的STACK來(lái)存儲(chǔ)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. 同時(shí)從q1/q2中取出節(jié)點(diǎn),算2者相加的結(jié)果(邏輯與Add Two Numbers一致)烘浦,算好的結(jié)果仍然放入到STACK結(jié)構(gòu)中
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中依次取出結(jié)果的每個(gè)節(jié)點(diǎn)抖坪,并組織成LinkedList
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while (!result.isEmpty()) {
head.next = result.pop();
head = head.next;
}
return dummy.next;
}
}