假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。
给定两个这种链表,请生成代表两个整数相加值的结果链表。
数据范围:
,链表任意值 
要求:空间复杂度
,时间复杂度 )
要求:空间复杂度
例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。
[9,3,7],[6,3]
{1,0,0,0}如题面解释
[0],[6,3]
{6,3}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
#include <stdlib.h>
int lenth(struct ListNode* p){
int num =0;
while(p != NULL){
num++;
p = p->next;
}
return num;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
int len1, len2, i, j, min = 0;
len1 = lenth(head1);
len2 = lenth(head2);
int* addend1 = (int*)calloc(len1, sizeof(int));
int* addend2 = (int*)calloc(len2, sizeof(int));
int* sum = (int*)calloc(len1+len2, sizeof(int));
struct ListNode* p1 = head1, *p2 = head2;
//将两个加数放入对应数组
for(i = len1 - 1; i >= 0; i--){
addend1[i] = p1->val;
p1 = p1->next;
}
for(i = len2 - 1; i >= 0; i--){
addend2[i] = p2->val;
p2 = p2->next;
}
p1 = head1;
p2 = head2;
//合并两个链表
while(p1->next != NULL){
p1 = p1->next;
}
p1->next = p2;
p1 = head1;
//单个计算对应位加和放入sum数组
if(len1 <= len2){
min = len1;
}else{
min = len2;
}
for(i = 0; i < min; i++){
sum[i] = addend1[i] + addend2[i];
}
j = i;
while(j != len1){
sum[j] = addend1[j];
j++;
}
j = i;
while(j != len2){
sum[j] = addend2[j];
j++;
}
//遍历数组,若发现本位和/10 == 1 说明有进位flag,默认没有进位
int flag = 0;
for(i = 0; i < len1 + len2; i++){
sum[i] += flag;
if(sum[i] / 10 == 1){
flag = 1;
sum[i] = sum[i] % 10;
}else{
flag = 0;
}
}
int dir = 0;//第一个不为0的位置(从0开始)
i = len1 + len2 -1;
while(i >= 0){
if(sum[i] == 0){
i--;
}else{
dir = i;
break;
}
}
i = dir;
while(i >= 0){
p1->val = sum[i];
if(i == 0){
p1->next = NULL;
}else{
p1 = p1->next;
}
i--;
}
return head1;
} struct ListNode* ReverseList(struct ListNode* head ) {
struct ListNode *p,*res;
if((head==NULL) || (head->next==NULL))
return head;
p = head->next;
res = ReverseList(p);
p->next = head;
p->next->next = NULL;
return res;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
struct ListNode *p,*p1,*p2,*res=NULL;
int carry=0;
p1 = ReverseList(head1);
p2 = ReverseList(head2);
while(p1!=NULL || p2!=NULL) {
int t;
t = (p1==NULL?0:p1->val)+(p2==NULL?0:p2->val)+carry;
if(t>9) {
carry = t/10;
t %= 10;
}
else
carry = 0;
if(res==NULL) {
res = (struct ListNode *)malloc(sizeof(struct ListNode));
res->val = t;
res->next = NULL;
p = res;
}
else {
struct ListNode *NewNode;
NewNode = (struct ListNode *)malloc(sizeof(struct ListNode));
NewNode->val = t;
NewNode->next = NULL;
p->next = NewNode;
p = p->next;
}
if(p1!=NULL)
p1 = p1->next;
if(p2!=NULL)
p2 = p2->next;
};
res = ReverseList(res);
return res;
} struct ListNode*Reverse(struct ListNode* cur,struct ListNode*prev)
{
if(cur==NULL)return prev;
struct ListNode *temp=NULL;
temp=cur->next;
cur->next=prev;
return Reverse(temp,cur);
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
struct ListNode*p1=Reverse(head1,NULL);
struct ListNode*p2=Reverse(head2,NULL);
struct ListNode*ret=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode*p=ret;
int count=0;
while(p1!=NULL && p2!=NULL)
{
int temp=p1->val+p2->val+count;
p1->val=p1->val+p2->val+count;
if(p1->val>=10)
{
p1->val=p1->val-10;
}
count=temp/10;
ret->next=p1;
ret=ret->next;
p1=p1->next;
p2=p2->next;
}
struct ListNode* p3=p1?p1:p2;
while(p3!=NULL)
{
int temp=p3->val+count;
p3->val=p3->val+count;
if(p3->val>=10)
{
p3->val=p3->val-10;
}
count=temp/10;
ret->next=p3;
p3=p3->next;
ret=ret->next;
}
//缺少考虑count会带出来一个进位
struct ListNode*retu=NULL;
if(1==count)
{
struct ListNode*temp1=p->next;
struct ListNode*temp2=Reverse(temp1,NULL);
p->val=count;
p->next=temp2;
retu=p;
}
else { //count没有进位的情况
struct ListNode*temp1=p->next;
struct ListNode*temp2=Reverse(temp1,NULL);
retu=temp2;
}
return retu;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* head ) {
if (head == NULL) {
return NULL;
}
struct ListNode* next = NULL;
struct ListNode* prior = NULL;
while (head != NULL){
next = head->next;//保存下一个节点
//头插法
head->next = prior;
prior = head;
head = next;
}
return prior ;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
struct ListNode* p1 = ReverseList(head1);
struct ListNode* p2 = ReverseList(head2);
struct ListNode* res = NULL;
struct ListNode* p = NULL;
int i =0;
while (p1!=NULL&&p2!=NULL) {
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->next = res;
if (p1->val+p2->val+i >= 10) {
p->val = p1->val+p2->val+i-10;
i = 1;
}else {
p->val = p1->val+p2->val+i;
i = 0;
}
res = p;
p1 = p1->next;
p2 = p2->next;
}
p = NULL;
while (p1!=NULL) {
if (p1->val+i>=10) {
p1->val = p1->val+i-10;
i=1;
}else {
p1->val = p1->val+i;
i = 0;
}
p = p1->next;
p1->next = res;
res = p1;
p1 = p;
}
while (p2!=NULL) {
if (p2->val+i>=10) {
p2->val = p2->val+i-10;
i=1;
}else {
p2->val = p2->val+i;
i = 0;
}
p = p2->next;
p2->next = res;
res = p2;
p2 = p;
}
if (i==1) {
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->val = 1;
p->next = res;
res = p;
}
return res;
} #include <stdio.h>
#include <stdlib.h>
int ListLen(struct ListNode* p){
int rtn = 0;
while(p!=NULL){
rtn = rtn+1;
p = p->next;
}
return rtn;
}
bool ListAdd(struct ListNode* ans,struct ListNode* p1,struct ListNode* p2, int len1,int len2){
bool upper = false;
if(ans!=NULL){
if(len1>=0){
ans->val += p1->val;
p1 = p1->next;
}
if(len2>=0){
ans->val += p2->val;
p2 = p2->next;
}
if(ListAdd(ans->next,p1,p2,len1+1,len2+1)){
ans->val+=1;
}
if(ans->val>=10){
ans->val -=10;
return true;
}
}
return false;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
struct ListNode* rtn = malloc(sizeof(struct ListNode)*1);
struct ListNode* tmp;
int len1 = ListLen(head1);
int len2 = ListLen(head2);
int i,rlen;
rlen = len1>len2?len1:len2;
rtn->val = 0;
rtn->next = NULL;
tmp = rtn;
for(i = 0;i < rlen;i++){
tmp->next = malloc(sizeof(struct ListNode));
tmp = tmp->next;
tmp->val = 0;
tmp->next = NULL;
}
ListAdd(rtn,head1,head2,len1-rlen-1,len2-rlen-1);
if(rtn->val == 0){
return rtn->next;
}
return rtn;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct ListNode* ReverseList(struct ListNode* pHead );
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
int i = 0;
struct ListNode *Head1 = ReverseList(head1);
struct ListNode *Head2 = ReverseList(head2);
struct ListNode *p = (struct ListNode *)malloc( sizeof(struct ListNode) );
struct ListNode *n,*Head = p;
p->next=NULL;
while (Head1&&Head2) {
n = (struct ListNode *)malloc( sizeof(struct ListNode) );
if((Head1->val + Head2->val + i)>=10)
{
n->val=(Head1->val + Head2->val + i)-10;
i=1;
}
else
{
n->val=(Head1->val + Head2->val + i);
i=0;
}
n->next=NULL;
p->next=n;
p=n;
Head1 = Head1->next;
Head2 = Head2->next;
}
if (i==0)
{
if (Head1) {
p->next=Head1;
}
else if (Head2){
p->next=Head2;
}
else;
}
while (Head1&&i)
{
n = (struct ListNode *)malloc( sizeof(struct ListNode) );
if((Head1->val + i)>=10)
{
n->val=(Head1->val + i)-10;
i=1;
n->next=NULL;
p->next=n;
p=n;
Head1 = Head1->next;
}
else
{
n->val=(Head1->val + i);
i=0;
p->next=n;
p=n;
p->next=Head1->next;
}
}
while (Head2&&i)
{
n = (struct ListNode *)malloc( sizeof(struct ListNode) );
if((Head2->val + i)>=10)
{
n->val=(Head2->val + i)-10;
i=1;
n->next=NULL;
p->next = n;
p = n;
Head2 = Head2->next;
}
else
{
n->val=(Head2->val + i);
i=0;
p->next=n;
p=n;
p->next=Head2->next;
}
}
if (i==1) {
n = (struct ListNode *)malloc( sizeof(struct ListNode) );
n->val=1;
n->next=NULL;
p->next = n;
p = n;
}
return ReverseList(Head->next);
}
struct ListNode* ReverseList(struct ListNode* pHead )
{
// write code here
if(pHead==NULL||pHead->next==NULL){
return pHead;
}
struct ListNode *cur = NULL;
cur = pHead;
struct ListNode *pre = NULL;
struct ListNode *temp;
while (cur) {
temp = cur->next;
cur->next=pre;
pre = cur;
cur = temp;
}
return pre;
} #include <stdio.h>
//翻转链表
struct ListNode* reverse(struct ListNode* head){
struct ListNode* cur = head;
struct ListNode* pre = NULL;
struct ListNode* nex = head->next;
while(cur)
{
cur->next = pre;
pre = cur;
cur = nex;
nex = cur->next;
}
return pre;
}
//链表相加
struct ListNode* addTwoNumbers(struct ListNode* head1, struct ListNode* head2){
struct ListNode* l1 = head1;
struct ListNode* l2 = head2;
struct ListNode* tail;
int n = 1;
int sum = 0;
while (l1 && l2) {
sum += (l1->val + l2->val) * n;
l1=l1->next;
l2=l2->next;
n=n*10;
}
while (l1==NULL && l2!=NULL) {
sum += l2->val * n;
l2=l2->next;
n=n*10;
}
while (l1!=NULL && l2==NULL) {
sum += l1->val * n;
l1=l1->next;
n=n*10;
}
// 头指针c
struct ListNode* c = NULL;
while (sum>=1) {
struct ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->val = sum % 10;
p->next = c;
c = p;
sum = sum/10;
}
return c;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
if (head1->val==0) {
return head2;
}
if (head2->val==0) {
return head1;
}
if (head1->next) {
head1 = reverse(head1);
}
if (head2->next) {
head2 = reverse(head2);
}
struct ListNode* head3;
head3 = addTwoNumbers(head1, head2);
return head3;
} #include <stdlib.h>
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
struct ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* q = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* tmp;
int sum = 0; //当前位
int flage = 0; //进位符号位
head->next = NULL;
p->next = NULL;
q->next = NULL;
//头插法逆置两个链表
while (head1 != NULL)
{
tmp = head1;
head1 = head1->next;
tmp->next = p->next;
p->next = tmp;
}
p = p->next;
while (head2 != NULL)
{
tmp = head2;
head2 = head2->next;
tmp->next = q->next;
q->next = tmp;
}
q = q->next;
while (1)
{
if(q != NULL && p != NULL)
{
sum = q->val + p->val + flage; //低位 符号位 相加
flage = sum >= 10 ? 1 : 0; //判断是否进位
sum %= 10; //进位后
p = p->next;
q = q->next;
}
else if (q != NULL && p == NULL)
{
sum = q->val + flage;
flage = sum >= 10 ? 1 : 0;
sum %= 10;
q = q->next;
}
else if (q == NULL && p != NULL)
{
sum = p->val + flage;
flage = sum >= 10 ? 1 : 0;
sum %= 10;
p = p->next;
}
else if(q == NULL && p == NULL && flage == 1)
{
sum = 1; //最高位只有进位
flage = 0;
}
else
{
break;
}
//头插法插入 每一位的结果sum
struct ListNode* new = (struct ListNode*)malloc(sizeof(struct ListNode));
new->val = sum;
new->next = head->next;
head->next = new;
}
return head->next;
} struct ListNode* reverse(struct ListNode* head){
struct ListNode* p1 = head->next;
struct ListNode* p2 = p1->next;
head->next = NULL;
while(p1){
p1->next = head;
head = p1;
p1 = p2;
if(p1)
p2 = p1->next;
}
return head;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode* head1 = l1;
struct ListNode* head2 = l2;
struct ListNode* tail;
while(head1 && head2){
tail = head1;
head1->val = head1->val + head2->val;
if(head1->val >= 10 && head1->next){
head1->val %= 10;
head1->next->val++;
}
else if(head1->val >= 10 && head2->next){
head1->val %= 10;
head2->next->val++;
head1->next = head2->next;
head2->next = NULL;
}
else if(head1->val >= 10){
head1->val %= 10;
l2->val = 1;
tail->next = l2;
l2->next = NULL;
}
head1 = head1->next;
head2 = head2->next;
}
while(head1){
if(head1->val >= 10){
head1->val %= 10;
if(head1->next)
head1->next->val++;
else{
l2->val = 1;
head1->next = l2;
l2->next = NULL;
}
}
head1 = head1->next;
}
while(head2)
{
tail->next = head2;
head2 = NULL;
}
return l1;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ){
if(head1->val == 0)
return head2;
if(head2->val == 0)
return head1;
if(head1->next)
head1 = reverse(head1);//逆置
if(head2->next)
head2 = reverse(head2);//逆置
struct ListNode* head3;
head3 = addTwoNumbers(head1, head2);//相加
if(head3->next)
head3 = reverse(head3);//结果逆置回来
return head3;
} typedef struct ListNode* pnode;
// 将两个链表的值求和
void mycombine(pnode head1,pnode head2,int len1,int len2){
pnode p=head1;
pnode q=head2;
while(len1>len2){
p=p->next;
len1--;
}//保持后对齐
while(p){
p->val+=q->val;
p=p->next;
q=q->next;
}
}
int carrybit(pnode head){ //实现进位操作
if(head==NULL)return 0;
head->val+=carrybit(head->next);
if(head->val>=10){
head->val-=10;
return 1;
}else return 0;
}
int mystrlen(pnode head){//计算链表得长度
int count=0;
while(head){
head=head->next;
count++;
}
return count;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
if(head1==NULL) return head1;
if(head2==NULL) return head2;
int len1=mystrlen(head1);
int len2=mystrlen(head2);
pnode temp=NULL;
//找到较大的链表,求和得到的放入其中;
if(len1>=len2){
temp=head1;
mycombine(temp,head2,len1,len2);//两个链表的值加到temp里
}else{
temp=head2;
mycombine(temp,head1,len2,len1);//两个链表的值加到temp里
}
int i=carrybit(temp);
if(i==1){
pnode newhead=(pnode)malloc(sizeof(struct ListNode));
newhead->val=1;
newhead->next=temp;
temp=newhead;
}
return temp;
} struct ListNode* ReverseList(struct ListNode* pHead )
{
struct ListNode *p = pHead;
struct ListNode *q = NULL;
struct ListNode *temp = NULL;
while(p)
{
temp = q;
q = p;
p = p->next;
q->next = temp;
}
// write code here
return q;
}
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 )
{
struct ListNode *p = ReverseList(head1);
struct ListNode *newhead = p;
struct ListNode *q = ReverseList(head2);
struct ListNode *temp = NULL;
int jin = 0;
while(p && q)
{
if(p->val + q->val + jin <= 9)
{
p->val = p->val + q->val + jin;
jin = 0;
}
else
{
p->val = (p->val + q->val +jin)%10;
jin = 1;
}
temp = p;
p = p->next;
q = q->next;
}
while(p || q || jin)
{
if(p)
{
if(p->val +jin <= 9)
{
p->val = p->val +jin;
jin = 0;
}
else
{
p->val = 0;
jin = 1;
}
temp = p;
p = p->next;
}
else if(q)
{
struct ListNode * newp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp->next = newp; // 最后一个结点的下一个 是 新结点
newp->next = NULL;
if(q->val +jin <= 9)
{
newp->val = q->val +jin;
jin = 0;
}
else
{
newp->val = 0;
jin = 1;
}
temp = newp;
q = q->next;
}
else if(jin)
{
struct ListNode * newp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp->next = newp; // 最后一个结点的下一个 是 新结点
newp->next = NULL;
newp->val = jin;
jin = 0;
}
}
return ReverseList(newhead);
// write code here
} int Recursion(struct ListNode* head1,struct ListNode* head2,int cout);
int LengthListNode(struct ListNode* pHead);
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2) {
// write code here
int len1=LengthListNode(head1);
int len2=LengthListNode(head2);
int sub=len1>len2?len1-len2:len2-len1;
struct ListNode* nh=(struct ListNode*)malloc(sizeof(struct ListNode));
int cout=0;
if(sub==0){
cout=Recursion(head1,head2,0);;
if(cout!=0){
nh->next=head1;nh->val=cout;
return nh;
}else{
return head1;
}
}
struct ListNode* sp=0;
struct ListNode* ep=0;
while(sub){ //补零序列
ep=(struct ListNode*)malloc(sizeof(struct ListNode));ep->val=0;
ep->next=sp;sp=ep;sub=sub-1;
}
//ep指向零结点的最后结点
ep=sp;
while(ep->next!=0){
ep=ep->next;
}
//连接上长度小的头结点
if(len1>len2){
ep->next=head2;
cout=Recursion(sp,head1,0); //存在sp中
}else{
ep->next=head1;
cout=Recursion(sp,head2,0); //存在sp中
}
//根据cout来进行输出
if(cout!=0){ //存在进位
nh->val=cout;nh->next=sp;
return nh;
}else{ //不存在进位
return sp;
}
}
int LengthListNode(struct ListNode* pHead){
int data=0;
while(pHead!=0){
pHead=pHead->next;data++;
}
return data;
}
int Recursion(struct ListNode* head1,struct ListNode* head2,int cout){
if(head1->next==0&&head2->next==0){
int result=head1->val+head2->val+cout;
head1->val=result%10;
return result/10;
}
int ct=Recursion(head1->next,head2->next,cout);
int result=head1->val+head2->val+ct;
head1->val=result%10;
return result/10;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
#先反转链表,再竖式相加
#include<stdlib.h>
struct ListNode * reverseListnode(struct ListNode *head)
{
struct ListNode *p, *q, *temp;
if(head ==NULL) return NULL;
if(head->next == NULL) return head;
p = head;
q = head->next;
head->next = NULL;
while(q != NULL)
{
temp = q->next;
q->next = p;
p = q;
q = temp;
}
return p;
}
struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2 ) {
// write code here
if(head1 == NULL && head2 == NULL) return NULL;
struct ListNode *tail1, *tail2;
tail1 = reverseListnode(head1);
tail2 = reverseListnode(head2);
int flag=0;
struct ListNode *p, *q, *head, *temp;
p = tail1;
q = tail2;
temp = NULL;
while(p != NULL && q != NULL)
{
head = (struct ListNode *) malloc(sizeof(struct ListNode));
head->val = p->val + q->val;
if(flag)
{
head->val ++;
flag = 0;
}
if(head->val > 9)
{
head->val = head ->val -10;
flag = 1;
}
head->next = temp;
temp = head;
p = p->next;
q = q->next;
}
while(p != NULL)
{
head = (struct ListNode *) malloc(sizeof(struct ListNode));
head->val = p->val;
if(flag)
{
head->val ++;
flag = 0;
}
if(head->val > 9)
{
head->val = head ->val -10;
flag = 1;
}
head->next = temp;
temp = head;
p = p->next;
}
while(q != NULL)
{
head = (struct ListNode *) malloc(sizeof(struct ListNode));
head->val = q->val;
if(flag)
{
head->val ++;
flag = 0;
}
if(head->val > 9)
{
head->val = head ->val -10;
flag = 1;
}
head->next = temp;
temp = head;
q = q->next;
}
if(flag)
{
head = (struct ListNode *) malloc(sizeof(struct ListNode));
head->val = 1;
head->next = temp;
}
tail1->next = NULL;
tail2->next = NULL;
return head;
}