错误解法:
#include <stdio.h>
#include <algorithm>
struct ListNode
{
int val;
struct ListNode *next;
};
typedef struct ListNode Node;
typedef struct ListNode *List_Pointer;
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
// List_Pointer head1=(List_Pointer)malloc(sizeof(Node));
// List_Pointer head2=(List_Pointer)malloc(sizeof(Node));
// head1=l1;
// head2=l2;
// List_Pointer tail1=(List_Pointer)malloc(sizeof(Node));
// List_Pointer tail2=(List_Pointer)malloc(sizeof(Node));
List_Pointer head1,head2,head3,tail1,tail1_prior,tail2,tail2_prior,tail3,p1,p2,p3;
p1=head1=l1;
p2=head2=l2;
tail1_prior=l1;
tail2_prior=l2;
tail1=l1;
tail2=l2;
head3=(List_Pointer)malloc(sizeof(Node));
head3->next=NULL;
while(tail1_prior!=NULL&&tail2_prior!=NULL)//结束条件
{
p1=head1;
p2=head2;
tail1_prior=NULL;
tail2_prior=NULL;
while(p1!=NULL)
{
if(p1->next!=NULL)
{
tail1_prior=p1;
p1=p1->next;
}
else
{
tail1=p1;
p1=p1->next;
}
}
while(p2!=NULL)
{
if(p2->next!=NULL)
{
tail2_prior=p2;
p2=p2->next;
}
else
{
tail2=p2;
p2=p2->next;
}
}
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
q->val=tail1->val+tail2->val;
q->next=head3->next;//插入第一个结点的时候就能保证最后一个结点的next为NULL。
head3->next=q;
if(tail1_prior!=NULL)//加一个判断条件。因为前面有tail1_prior=NULL;
{
tail1_prior->next=NULL;//链没有断掉。
free(tail1);//此时tail->val的值从自己给定的变为随机值了。
tail1=NULL;
}
else
{
free(tail1);//此时tail->val的值从自己给定的变为随机值了。
tail1=NULL;
}
if(tail2_prior!=NULL)
{
tail2_prior->next=NULL;
free(tail2);
tail2=NULL;
}
else
{
free(tail1);//此时tail->val的值从自己给定的变为随机值了。
tail1=NULL;
}
}
while(tail1_prior!=NULL)//结束条件
{
p1=head1;
tail1_prior=NULL;
while(p1!=NULL)
{
if(p1->next!=NULL)
{
tail1_prior=p1;
p1=p1->next;
}
else
{
tail1=p1;
p1=p1->next;
}
}
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
q->val=tail1->val;
q->next=head3->next;//插入第一个结点的时候就能保证最后一个结点的next为NULL。
head3->next=q;
if(tail1_prior!=NULL)//加一个判断条件。因为前面有tail1_prior=NULL;
{
tail1_prior->next=NULL;//链没有断掉。
free(tail1);//此时tail->val的值从自己给定的变为随机值了。
tail1=NULL;
}
else
{
free(tail1);//此时tail->val的值从自己给定的变为随机值了。
tail1=NULL;
}
}
while(tail2_prior!=NULL)//结束条件
{
p2=head2;
tail2_prior=NULL;
while(p2!=NULL)
{
if(p2->next!=NULL)
{
tail2_prior=p2;
p2=p2->next;
}
else
{
tail2=p2;
p2=p2->next;
}
}
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
q->val=tail2->val;
q->next=head3->next;//插入第一个结点的时候就能保证最后一个结点的next为NULL。
head3->next=q;
if(tail2_prior!=NULL)//加一个判断条件。因为前面有tail1_prior=NULL;
{
tail2_prior->next=NULL;//链没有断掉。
free(tail2);//此时tail->val的值从自己给定的变为随机值了。
tail2=NULL;
}
else
{
free(tail2);//此时tail->val的值从自己给定的变为随机值了。
tail1=NULL;
}
}
return head3->next;
}
//不带头结点的尾插法
void Tail_Insert_List(List_Pointer &head,List_Pointer &tail,int val)//&tail。不加&的话每次的tail的值还是head的值
{
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
if(tail!=NULL)
{
q->val=val;
tail->next=q;
tail=tail->next;
tail->next=NULL;
}
else
{
q->val=val;
tail=q;
tail->next=NULL;
head=tail;//注意这里。不这样的话主函数的head仍然是0。
}
}
void printf_List(List_Pointer p)
{
while(p!=NULL)
{
if(p->next!=NULL)
{
//printf("%d(地址=%p)->",p->val,p);
printf("%d->",p->val,p);
p=p->next;
}
else
{
printf("%d",p->val,p);
p=p->next;
}
}
}
int main()
{
int val;
List_Pointer head1=(List_Pointer)malloc(sizeof(Node));
head1=NULL;
List_Pointer tail1=(List_Pointer)malloc(sizeof(Node));
tail1=head1;
while(scanf("%d",&val)!=EOF)
{
Tail_Insert_List(head1,tail1,val);
}
printf_List(head1);
printf("\n");
List_Pointer head2=(List_Pointer)malloc(sizeof(Node));
head2=NULL;
List_Pointer tail2=(List_Pointer)malloc(sizeof(Node));
tail2=head2;
while(scanf("%d",&val)!=EOF)
{
Tail_Insert_List(head2,tail2,val);
}
printf_List(head2);
printf("\n");
printf("\n");
List_Pointer head3;
head3=addTwoNumbers(head1,head2);
printf_List(head3);
return 0;
}
做到一半发现理解错题意了。
位数是按照 逆序 的方式存储的
我以为是顺序方式存储。
#include <stdio.h>
#include <algorithm>
struct ListNode
{
int val;
struct ListNode *next;
};
typedef struct ListNode Node;
typedef struct ListNode *List_Pointer;
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
List_Pointer head1,head2,head3,p1,p2,p3;
p1=head1=l1;
p2=head2=l2;
p3=head3=(List_Pointer)malloc(sizeof(Node));
head3->next=NULL;
bool jinwei_flag=false;
while(head1!=NULL&&head2!=NULL)
{
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
if(jinwei_flag==false)
{
q->val=head1->val+head2->val;
if(q->val>=0&&q->val<10)
{
jinwei_flag=false;
p3->next=q;
p3=p3->next;
p3->next=NULL;
}
else if(q->val>=10)
{
q->val=q->val-10;
jinwei_flag=true;
p3->next=q;
p3=p3->next;
p3->next=NULL;
}
head1=head1->next;
head2=head2->next;
}
else//jinwei_flag==true
{
q->val=head1->val+head2->val;
q->val=q->val+1;
if(q->val>=0&&q->val<10)
{
jinwei_flag=false;
p3->next=q;
p3=p3->next;
p3->next=NULL;
}
else if(q->val>=10)
{
q->val=q->val-10;
jinwei_flag=true;
p3->next=q;
p3=p3->next;
p3->next=NULL;
}
head1=head1->next;
head2=head2->next;
}
}
while(head1!=NULL)
{
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
if(jinwei_flag==true)
{
q->val=head1->val;
q->val=q->val+1;
if(q->val>=0&&q->val<10)
{
jinwei_flag=false;
p3->next=q;
p3=p3->next;
p3->next=NULL;
head1=head1->next;
}
else
{
jinwei_flag=true;
q->val=q->val-10;
p3->next=q;
p3=p3->next;
p3->next=NULL;
head1=head1->next;
}
}
else
{
q->val=head1->val;
p3->next=q;
p3=p3->next;
p3->next=NULL;
head1=head1->next;
}
}
while(head2!=NULL)
{
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
if(jinwei_flag==true)
{
q->val=head2->val;
q->val=q->val+1;//1,9 9
if(q->val>=0&&q->val<10)
{
jinwei_flag=false;
p3->next=q;
p3=p3->next;
p3->next=NULL;
head2=head2->next;
}
else
{
jinwei_flag=true;
q->val=q->val-10;
p3->next=q;
p3=p3->next;
p3->next=NULL;
head2=head2->next;
}
}
else
{
q->val=head2->val;
p3->next=q;
p3=p3->next;
p3->next=NULL;
head2=head2->next;
}
}
if(jinwei_flag==true)//5,5的情况。根据没通过的用例来调整代码。但是这样[9,8][1]又没通过了
{
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
q->val=1;
p3->next=q;
p3=p3->next;
p3->next=NULL;
}
return head3->next;
}
//不带头结点的尾插法
void Tail_Insert_List(List_Pointer &head,List_Pointer &tail,int val)//&tail。不加&的话每次的tail的值还是head的值
{
List_Pointer q=(List_Pointer)malloc(sizeof(Node));
if(tail!=NULL)
{
q->val=val;
tail->next=q;
tail=tail->next;
tail->next=NULL;
}
else
{
q->val=val;
tail=q;
tail->next=NULL;
head=tail;//注意这里。不这样的话主函数的head仍然是0。
}
}
void printf_List(List_Pointer p)
{
while(p!=NULL)
{
if(p->next!=NULL)
{
//printf("%d(地址=%p)->",p->val,p);
printf("%d->",p->val,p);
p=p->next;
}
else
{
printf("%d",p->val,p);
p=p->next;
}
}
}
int main()
{
int val;
List_Pointer head1=(List_Pointer)malloc(sizeof(Node));
head1=NULL;
List_Pointer tail1=(List_Pointer)malloc(sizeof(Node));
tail1=head1;
while(scanf("%d",&val)!=EOF)
{
Tail_Insert_List(head1,tail1,val);
}
printf_List(head1);
printf("\n");
List_Pointer head2=(List_Pointer)malloc(sizeof(Node));
head2=NULL;
List_Pointer tail2=(List_Pointer)malloc(sizeof(Node));
tail2=head2;
while(scanf("%d",&val)!=EOF)
{
Tail_Insert_List(head2,tail2,val);
}
printf_List(head2);
printf("\n");
printf("\n");
List_Pointer head3;
head3=addTwoNumbers(head1,head2);
printf_List(head3);
return 0;
}
运行结果:
根据没有通过的测试用例不断调整自己的代码。