Add Two Numbers
Category Difficulty Likes Dislikes
algorithms Medium (31.66%) 5950 1551
Tags
Companies
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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
自己的解法:执行用时:48 ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void displayList(ListNode *p1){
while (p1){
cout << p1->val<<" ";
p1 = p1->next;
}
cout << endl;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int sign = 0;
vector<int> dataqueue;
bool flagsign = false;
while (l1&&l2){
int data = l1->val + l2->val;
if (flagsign){
data += sign;
flagsign = false;
}
if (data >= 10){
sign = data / 10;
data %= 10;
flagsign = true;
}
dataqueue.push_back(data);
l1 = l1->next;
l2 = l2->next;
}
while (l1){
int data = l1->val;
if (flagsign){
data += sign;
flagsign = false;
}
if (data >= 10){
sign = data / 10;
data %= 10;
flagsign = true;
}
dataqueue.push_back(data);
l1 = l1->next;
}
while (l2){
int data = l2->val;
if (flagsign){
data += sign;
flagsign = false;
}
if (data >= 10){
sign = data / 10;
data %= 10;
flagsign = true;
}
dataqueue.push_back(data);
l2 = l2->next;
}
if (sign > 0 && flagsign)
dataqueue.push_back(sign);
ListNode *head = new ListNode(dataqueue[0]);
ListNode *pp1 = head;
for (unsigned int i = 1; i < dataqueue.size(); ++i){
ListNode *temp = new ListNode(dataqueue[i]);
head->next = temp;
head = temp;
}
//ptemp = createList(dataqueue);
ListNode *ppp = pp1;
displayList(ppp);
return pp1;
}
/**之前的思路,认为借助stack进行反转,然后进行相加之后,再将值放回stack,再建立ListNode
if(NULL==pp){
cout<<"ListNode is NULL"<<endl;
return;
}
while(pp){
dataStack.push(pp->val);
pp=pp->next;
}
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> stack01;
stack<int> stack02;
insertData(l1,stack01);
insertData(l2,stack02);
int data01=0;
int data02=0;
while(!stack01.empty()){
int temp1 = stack01.top();
//cout<<temp1<<endl;
data01 = data01*10+temp1;
//cout<<"111:"<<data01<<endl;
stack01.pop();
}
//cout<<data01<<endl;
while(!stack02.empty()){
int temp2 = stack02.top();
//cout<<temp2<<endl;
data02 = data02*10+temp2;
//cout<<"222:"<<data02<<endl;
stack02.pop();
}
//cout<<data02<<endl;
int result = data01+data02;
cout<<"result:"<<result<<endl;
int num=result%10;
result/=10;
cout<<num<<" "<<result<<endl;
ListNode head(num);
cout<<"111"<<endl;
pp=head;
ListNode *p1=&head;
cout<<"2222"<<endl;
while(result!=0){
cout<<"333"<<endl;
num=result%10;
cout<<"num:"<<num<<endl;
ListNode temp(num);
head.next=&temp;
head = temp;
result/=10;
cout<<"result:"<<result<<endl;
}
while(p1){
cout<<p1->val<<endl;;
p1 = p1->next;
}
return &pp;
}*/
};
看leetcode网站上,别人开发的耗时较短的程序:
执行用时为 16 ms 的范例
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* num1Bit;
ListNode* num2Bit;
num1Bit = l1;
num2Bit = l2;
ListNode* last = NULL;
ListNode* output = NULL;
int up = 0;
while(num1Bit != NULL || num2Bit != NULL) {
int ans = up;
if (num1Bit) {
ans += num1Bit->val;
num1Bit = num1Bit->next;
}
if (num2Bit) {
ans += num2Bit->val;
num2Bit = num2Bit->next;
}
if (ans >= 10) {
ans -= 10;
up = 1;
} else {
up = 0;
}
ListNode* ansNode = new ListNode(ans);
if (last) {
last->next = ansNode;
} else {
output = ansNode;
}
last = ansNode;
}
if (up != 0) {
ListNode* ansNode = new ListNode(1);
last->next = ansNode;
}
return output;
}
};
执行用时为 20 ms 的范例
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = new ListNode(-1), *cur = head;
int carry=0;
while(l1 || l2){
int val1= l1? l1->val:0;
int val2= l2? l2->val:0;
int sum=val1+val2+carry;
carry=sum/10;
cur->next=new ListNode(sum%10);
cur=cur->next;
if(l1) l1=l1->next;
if(l2) l2=l2->next;
}
if(carry) cur->next=new ListNode(1);
return head->next;
}
};