<p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;"></p><pre name="code" class="cpp"><span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255);">You are given two linked lists representing two non-negative numbers. 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.</span>
<span style="background-color: rgb(255, 255, 255); box-sizing: border-box; font-weight: 700;">Input:</span><span style="background-color: rgb(255, 255, 255);"> (2 -> 4 -> 3) + (5 -> 6 -> 4)</span>
Output: 7 -> 0 -> 8
思路,相加结果插入结果列表的最前面,记得处理进位。
#include "malloc.h"
#include "iostream"
#include <vector>
#include <string>
using namespace std;
struct node
{
int num;
node *next;
/* data */
};//声明节点
node *addTwoNum(node* link1,node *link2){
node *resultLinkHeadPtr=(node*)malloc(sizeof(node));
resultLinkHeadPtr->num=0;
resultLinkHeadPtr->next=NULL;
node *curNode1=link1->next;
node* curNode2=link2->next;
int carry=0;//进位标识
while(curNode1!=NULL && curNode2!=NULL){//都不为空 开始相加
int result=curNode1->num+curNode2->num+carry;
carry=0;
if (result>9)
{
carry=1;
result=result%10;
/* code */
}
node* tempResult=(node*)malloc(sizeof(node));//下面思路从翻转链表得来
tempResult->num=result;
tempResult->next=resultLinkHeadPtr->next;
resultLinkHeadPtr->next=tempResult;
curNode1=curNode1->next;
curNode2=curNode2->next;
}
while(curNode1!=NULL){//
int result=curNode1->num+carry;
carry=0;
if (result>9)
{
result=result%10;
carry=1;
/* code */
}
node* tempResult=(node*)malloc(sizeof(node));
tempResult->num=result;
tempResult->next=resultLinkHeadPtr->next;
resultLinkHeadPtr->next=tempResult;
curNode1=curNode1->next;
}
while(curNode2!=NULL){
int result=curNode2->num+carry;
carry=0;
if (result>9)
{
result=result%10;
carry=1;
/* code */
}
node* tempResult=(node*)malloc(sizeof(node));
tempResult->num=result;
tempResult->next=resultLinkHeadPtr->next;
resultLinkHeadPtr->next=tempResult;
curNode2=curNode2->next;
}
if(carry){//处理进位
node* tempResult=(node*)malloc(sizeof(node));
tempResult->num=carry;
tempResult->next=resultLinkHeadPtr->next;
resultLinkHeadPtr->next=tempResult;
}
return resultLinkHeadPtr;
}
int main(){
//make list
// std::cout<<"fuck";
node* Link1HeadPtr=(node*)malloc(sizeof(node));
Link1HeadPtr->num=0;//num 在此处没有作用,初始化为0
Link1HeadPtr->next=NULL;
//num is 527
node* Link1node1=(node*)malloc(sizeof(node));
Link1node1->num=0;
Link1HeadPtr->next=Link1node1;
node* Link1node2=(node*)malloc(sizeof(node));
Link1node2->num=0;
Link1node1->next=Link1node2;
node* Link1node3=(node*)malloc(sizeof(node));
Link1node3->num=0;
Link1node2->next=Link1node3;
node* Link1node4=(node*)malloc(sizeof(node));
Link1node4->num=0;
Link1node3->next=Link1node4;
Link1node4->next=NULL;
node* Link2HeadPtr=(node*)malloc(sizeof(node));
Link2HeadPtr->num=0;//num 在此处没有作用,初始化为0
Link2HeadPtr->next=NULL;
//num is 4931
node* Link2node1=(node*)malloc(sizeof(node));
Link2node1->num=9;
Link2HeadPtr->next=Link2node1;
node* Link2node2=(node*)malloc(sizeof(node));
Link2node2->num=9;
Link2node1->next=Link2node2;
node* Link2node3=(node*)malloc(sizeof(node));
Link2node3->num=9;
Link2node2->next=Link2node3;
node* Link2node4=(node*)malloc(sizeof(node));
Link2node4->num=9;
Link2node3->next=Link2node4;
Link2node4->next=NULL;
// cout<<Link1HeadPtr<<"fuck";
node* addResult= addTwoNum(Link1HeadPtr,Link2HeadPtr);
node *lastResult=addResult->next;
// cout<<ss->next<<"NULL";
while(lastRest!=NULL){
cout<<lastResult->num<<" ";
lastRest=lastResult->next;
}
// need delete all ptr;
return 0;
}