示例1:
示例2:
示例3:
package Stack;
import java.util.ArrayDeque;
import java.util.Deque;
public class p445_AddingTwoNumbersII {
int val;
p445_AddingTwoNumbersII next;
public p445_AddingTwoNumbersII(int val) {
this.val = val;
}
public p445_AddingTwoNumbersII(int val, p445_AddingTwoNumbersII next) {
this.val = val;
this.next = next;
}
public static void main(String[] args) {
p445_AddingTwoNumbersII l11 = new p445_AddingTwoNumbersII(7);
p445_AddingTwoNumbersII l12 = new p445_AddingTwoNumbersII(2);
p445_AddingTwoNumbersII l13 = new p445_AddingTwoNumbersII(4);
p445_AddingTwoNumbersII l14 = new p445_AddingTwoNumbersII(3);
p445_AddingTwoNumbersII l21 = new p445_AddingTwoNumbersII(5);
p445_AddingTwoNumbersII l22 = new p445_AddingTwoNumbersII(6);
p445_AddingTwoNumbersII l23 = new p445_AddingTwoNumbersII(4);
l11.next = l12;
l12.next = l13;
l13.next = l14;
l21.next = l22;
l22.next = l23;
p445_AddingTwoNumbersII res = addTwoNumbers(l11, l21);
while (res != null) {
if (res.next != null) {
System.out.print(res.val + "->");
} else {
System.out.print(res.val);
}
res = res.next;
}
}
public static p445_AddingTwoNumbersII addTwoNumbers(p445_AddingTwoNumbersII l1, p445_AddingTwoNumbersII l2) {
Deque<Integer> stack1 = new ArrayDeque<Integer>();
Deque<Integer> stack2 = new ArrayDeque<Integer>();
int carry = 0;
p445_AddingTwoNumbersII head = null;
while (l1 != null) {
stack1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
stack2.push(l2.val);
l2 = l2.next;
}
while (!stack1.isEmpty() || !stack2.isEmpty() || carry != 0) {
int x = stack1.isEmpty() ? 0 : stack1.pop();
int y = stack2.isEmpty() ? 0 : stack2.pop();
int sum = x + y + carry;
carry = sum / 10;
p445_AddingTwoNumbersII temp = new p445_AddingTwoNumbersII(sum % 10);
temp.next = head;
head = temp;
}
return head;
}
}
#include
#include
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
int stack1[101] = { 0 };
int stack2[101] = { 0 };
int top1 = 0;
int top2 = 0;
int carry = 0;
struct ListNode* temp = NULL;
struct ListNode* head = NULL;
while (l1 != NULL)
{
stack1[top1++] = l1->val;
l1 = l1->next;
}
while (l2 != NULL)
{
stack2[top2++] = l2->val;
l2 = l2->next;
}
while (top1 != 0 || top2 != 0 || carry != 0)
{
int x = top1 > 0 ? stack1[--top1] : 0;
int y = top2 > 0 ? stack2[--top2] : 0;
int sum = x + y + carry;
carry = sum / 10;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
head->val = sum % 10;
head->next = temp;
temp = head;
}
return head;
}
/*主函数省略*/
Java语言版
C语言版