给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int value;
struct Node *next;
}SNode;
SNode* addTwoNumbers(SNode *head ,SNode *head_1){
SNode *p = (SNode*)malloc(sizeof(SNode));
SNode *p1 = p;
int add;
while(head != NULL || head_1 != NULL){
SNode *p2 = (SNode*)malloc(sizeof(SNode));
int a = 0;
int b = 0;
if(head!=NULL)
a = head->value;
if(head_1!=NULL)
b = head_1->value;
int num = a+b+add;
p2->value = num % 10;
add = num / 10;
p2->next = NULL;
p1->next = p2;
p1 = p2;
if(head!=NULL)
head = head->next;
if(head_1!=NULL)
head_1 = head_1->next;
}
if(add !=0){
SNode *p2 = (SNode*)malloc(sizeof(SNode));
p2->value = add;
p2->next = NULL;
p1->next = p2;
}
return p->next;
}
int main() {
SNode *head = (SNode*)malloc(sizeof(SNode));
int a[] = {5};
int b[] = {5};
//引用指针 做一个变量
SNode *p2 = NULL;
for (int i = 0; i < 1; ++i) {
if(i == 0){
head->value = a[i];
head->next = NULL;
p2 = head;
} else{
SNode *p = (SNode*)malloc(sizeof(SNode));
p->value = a[i];
p->next = NULL;
//上一个的next 指向这一个
p2->next = p;
//把这一个p当上一个节点
p2=p;
}
}
SNode *head_1 = (SNode*)malloc(sizeof(SNode));
//引用指针 做一个变量
SNode *p3 = NULL;
for (int i = 0; i < 1; ++i) {
if(i == 0){
head_1->value = b[i];
head_1->next = NULL;
p3 = head_1;
} else{
SNode *p = (SNode*)malloc(sizeof(SNode));
p->value = b[i];
p->next = NULL;
//上一个的next 指向这一个
p3->next = p;
//把这一个p当上一个节点
p3=p;
}
}
SNode *p4 = addTwoNumbers(head,head_1);
while(p4)
{
printf("%d ",p4->value);
p4=p4->next;
}
printf("\n");
return 0;
}