题目
难度 Medium
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
实现思路
刚开始没想到可以反转链表来计算进位,不知道怎么才能从高位开始计算进位,而我的递归算法力基本为零,也不会用递归做。后来看到Follow Up说如果不反转应该怎么做,才知道原来可以反转链表(投机取巧了一下),就可以比较容易计算进位了。基本思路就是先把两个链表的数字用数组存起来,然后再开一个数组把每一位对应相加,但是是按最低位到最高位的顺序保存,即最低位的下标最小,最高位的下标最大。然后再遍历一次该数组,处理进位问题,如果该位上数值大于9,则该位数字-10,下一位进1。这一次遍历也可以省去,在把链表每一位对应相加时处理即可。但是比较方便看代码我就没有省。然后再从数组末端,即最高位开始构建答案链表。
时间复杂度和空间复杂度都为O(max { length(L1), length(L2)} )。
实现代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
// pnext,qnext用于遍历两个链表
ListNode* pnext = l1;
ListNode* qnext = l2;
//count1,count2用于记录两个链表的长度
int count1 = 0;
int count2 = 0;
//数组用于记录两个链表每一位的数值
int array1[100], array2[100];
while (pnext != NULL) {
array1[count1++] = pnext->val;
pnext = pnext->next;
}
while (qnext != NULL) {
array2[count2++] = qnext->val;
qnext = qnext->next;
}
//用于记录两个链表对应位相加的结果(未进位)
int arrayTemp[100] = {0};
//arrayTemp的下标
int index = 0;
//计算两个链表的对应位相加,最低位放在下标最小位
arrayTemp[index++] = array1[count1-1]+array2[count2-1];
if (count1 > count2) {
for (int i = count1-2; i >= 0; i--) {
if (i < count1-count2) {
arrayTemp[index++] = array1[i];
} else {
arrayTemp[index++] = array1[i]+array2[i-count1+count2];
}
}
} else {
for (int i = count2-2; i >= 0; i--) {
if (i < count2-count1) {
arrayTemp[index++] = array2[i];
} else {
arrayTemp[index++] = array1[i-count2+count1]+array2[i];
}
}
}
//处理进位
for (int i = 0; i < index; i++) {
if (arrayTemp[i] > 9) {
arrayTemp[i] -= 10;
arrayTemp[i+1] += 1;
}
}
//如果进位使得产生了更高位,则从更高位开始构建链表
if (arrayTemp[index] != 0) {
ListNode* answer = new ListNode(arrayTemp[index]);
pnext = answer;
for (int i = index-1; i >= 0; i--) {
ListNode* node = new ListNode(arrayTemp[i]);
pnext->next = node;
pnext = pnext->next;
}
return answer;
} else {
ListNode* answer = new ListNode(arrayTemp[index-1]);
pnext = answer;
for (int i = index-2; i >= 0; i--) {
ListNode* node = new ListNode(arrayTemp[i]);
pnext->next = node;
pnext = pnext->next;
}
return answer;
}
}
};
然后去观摩了别人的算法,发现只要利用栈先进后出的思想就可以了(对不起数据结构老师)。先遍历两个链表,开两个栈,然后按遍历顺序把链表每一位数字入栈,出栈时就是从低位到高位了!然后每次从两个栈中取出栈顶元素相加,再放入新的栈中,从新的栈中出栈的顺序就是从高位到低位了!非常便利!处理进位问题依旧可以用数组来做,但是也可以在放入新的栈时处理。
参考博客:http://www.cnblogs.com/grandyang/p/6216480.html
ps:该博客中还有其他算法。
实现代码2:
//不反转链表实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* pnext = l1;
ListNode* qnext = l2;
int count1 = 0;
int count2 = 0;
stack<int> stack1,stack2,stack_ans;
while (pnext != NULL) {
stack1.push(pnext->val);
pnext = pnext->next;
count1++;
}
while (qnext != NULL) {
stack2.push(qnext->val);
qnext = qnext->next;
count2++;
}
int num1,num2,sum,carry;
carry = 0;
if (count1>count2) {
while(!stack2.empty()) {
num1 = stack1.top();
stack1.pop();
num2 = stack2.top();
stack2.pop();
sum = (num1+num2+carry)%10;
carry = (num1+num2+carry)/10;
stack_ans.push(sum);
}
while (!stack1.empty()) {
num1 = stack1.top();
stack1.pop();
sum = (num1+carry)%10;
carry = (num1+carry)/10;
stack_ans.push(sum);
}
} else {
while(!stack1.empty()) {
num1 = stack1.top();
stack1.pop();
num2 = stack2.top();
stack2.pop();
sum = (num1+num2+carry)%10;
carry = (num1+num2+carry)/10;
stack_ans.push(sum);
}
while (!stack2.empty()) {
num2 = stack2.top();
stack2.pop();
sum = (num2+carry)%10;
carry = (num2+carry)/10;
stack_ans.push(sum);
}
}
//如果产生最高位的进位
if (carry != 0) stack_ans.push(carry);
ListNode* ans = NULL;
while (!stack_ans.empty()) {
ListNode* node = new ListNode(stack_ans.top());
if (ans == NULL) {
ans = new ListNode(stack_ans.top());
pnext = ans;
} else {
pnext->next = node;
pnext = pnext->next;
}
stack_ans.pop();
}
return ans;
}
};