题目描述
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
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码
利用reverse函数将问题转化为leetcode2.问题
#include <iostream>
using namespace std;
/// Using reverse
/// Time Complexity: O(m + n + max(m, n))
/// Space Complexity: O(1)
/// 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) {
l1 = reverse(l1);
l2 = reverse(l2);
ListNode* dummyHead = new ListNode(-1), *cur = dummyHead;
int carry = 0;
for(ListNode* node1 = l1, *node2 = l2; node1 || node2 || carry;
node1 = node1 ? node1->next : NULL, node2 = node2 ? node2->next : NULL){
int x = node1 ? node1->val : 0;
x += node2 ? node2->val : 0;
x += carry;
cur->next = new ListNode(x % 10);
cur = cur->next;
carry = x / 10;
}
return reverse(dummyHead->next);
}
private:
ListNode* reverse(ListNode* node){
if(!node->next) return node;
ListNode* ret = reverse(node->next);
node->next->next = node;
node->next = NULL;
return ret;
}
};
int main() {
return 0;
}
思路二
利用栈
#include <iostream>
#include <stack>
using namespace std;
/// Using Stack
/// Time Complexity: O(m + n + max(m, n))
/// Space Complexity: O(m + n)
/// 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) {
stack<ListNode*> stack1, stack2, stack;
ListNode* node = l1;
while(node) stack1.push(node), node = node->next;
node = l2;
while(node) stack2.push(node), node = node->next;
int carry = 0;
while(!stack1.empty() || !stack2.empty() || carry){
int x = 0;
if(!stack1.empty()) x += stack1.top()->val, stack1.pop();
if(!stack2.empty()) x += stack2.top()->val, stack2.pop();
x += carry;
stack.push(new ListNode(x % 10));
carry = x / 10;
}
ListNode* ret = stack.top(), *cur = ret;
stack.pop();
while(!stack.empty())
cur->next = stack.top(), cur = cur->next, stack.pop();
return ret;
}
};
int main() {
return 0;
}
思路三
有待学习
#include <iostream>
#include <stack>
using namespace std;
/// Recursion
/// Time Complexity: O(m + n + max(m, n))
/// Space Complexity: O(max(m, n))
/// 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) {
int len1 = get_length(l1), len2 = get_length(l2);
int len = max(len1, len2);
if(len1 != len2){
ListNode* head = len1 < len2 ? l1 : l2;
for(int i = 0; i < len - min(len1, len2); i ++){
ListNode* node = new ListNode(0);
node->next = head;
head = node;
}
if(len1 < len2) l1 = head;
else l2 = head;
}
int carry = 0;
ListNode* res = go(l1, l2, carry);
if(carry){
ListNode* node = new ListNode(1);
node->next = res;
res = node;
}
return res;
}
private:
int get_length(ListNode* head){
if(!head) return 0;
return 1 + get_length(head->next);
}
ListNode* go(ListNode* l1, ListNode* l2, int& carry){
if(!l1){
assert(!l2);
carry = 0;
return NULL;
}
int c = 0;
ListNode* next = go(l1->next, l2->next, c);
int x = l1->val + l2->val + c;
ListNode* res = new ListNode(x % 10);
res->next = next;
carry = x / 10;
return res;
}
};
int main() {
return 0;
}