给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode *head = NULL;
struct ListNode *newList = NULL;
struct ListNode *node = NULL;
int result = 0, flag = 0;
//newList = head;
while ((l1 != NULL) || (l2 != NULL))
{
if ((l1 != NULL) && (l2 != NULL))
{
result = l1->val + l2->val + flag;
l1 = l1->next;
l2 = l2->next;
}
else if ((l1 == NULL) && (l2 != NULL))
{
result = l2->val + flag;
l2 = l2->next;
}
else
{
result = l1->val + flag;
l1 = l1->next;
}
if (result >= 10)
{
result = result - 10;
flag = 1;
}
else
{
flag = 0;
}
node = (struct ListNode *)malloc(sizeof(struct ListNode ));
node->val = result;
node->next = NULL;
if (head == NULL)
{
head = node;
newList = head;
}
else
{
newList->next = node;
newList = newList->next;
}
}
if (flag == 1)
{
node = (struct ListNode *)malloc(sizeof(struct ListNode ));
node->val = flag;
node->next = NULL;
newList->next = node;
newList = newList->next;
}
return head;
}
The test code as follow:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode *head = NULL;
struct ListNode *newList = NULL;
struct ListNode *node = NULL;
int result = 0, flag = 0;
while ((l1 != NULL) || (l2 != NULL))
{
if ((l1 != NULL) && (l2 != NULL))
{
result = l1->val + l2->val + flag;
l1 = l1->next;
l2 = l2->next;
}
else if ((l1 == NULL) && (l2 != NULL))
{
result = l2->val + flag;
l2 = l2->next;
}
else
{
result = l1->val + flag;
l1 = l1->next;
}
if (result >= 10)
{
result = result - 10;
flag = 1;
}
else
{
flag = 0;
}
node = (struct ListNode *)malloc(sizeof(struct ListNode ));
node->val = result;
node->next = NULL;
if (head == NULL)
{
head = node;
newList = head;
}
else
{
newList->next = node;
newList = newList->next;
}
}
if (flag == 1)
{
node = (struct ListNode *)malloc(sizeof(struct ListNode ));
node->val = flag;
node->next = NULL;
newList->next = node;
newList = newList->next;
}
return head;
}
int main()
{
struct ListNode *node1=NULL, *node2=NULL;
struct ListNode *head1 = NULL;
struct ListNode *head2 = NULL;
struct ListNode *head1_next = NULL;
struct ListNode *head2_next = NULL;
struct ListNode *result = NULL;
int i = 0;
for (i = 0; i < 8; i++)
{
node1 = (struct ListNode *)malloc(sizeof(struct ListNode ));
node2 = (struct ListNode *)malloc(sizeof(struct ListNode ));
node1->val = i;
node1->next = NULL;
node2->val = i + 1;
node2->next = NULL;
if (i == 0)
{
head1 = node1;
head2 = node2;
head1_next = head1;
head2_next = head2;
}
else
{
head1_next->next = node1;
head2_next->next = node2;
head1_next = head1_next->next;
head2_next = head2_next->next;
}
}
node1 = head1;
node2 = head2;
printf("The input list node is :");
while (node1 != NULL)
{
printf(" %d %d ", node1->val, node2->val);
node1 = node1->next;
node2 = node2->next;
}
printf("\n");
result = addTwoNumbers(head1, head2);
node1=result;
printf("the result is");
while(node1 != NULL)
{
printf(" %d ",node1->val);
node1=node1->next;
}
printf("\n");
return 0;
}
The result is :
hanwu@hanwu:/media/hanwu/Software/work/leetcode/addTwoNumber$ gcc addTwoNumbers.c
hanwu@hanwu:/media/hanwu/Software/work/leetcode/addTwoNumber$ ./a.out
The input list node is : 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8
the result is 1 3 5 7 9 1 4 6 1
hanwu@hanwu:/media/hanwu/Software/work/leetcode/addTwoNumber$