您的位置:首页 > 健康 > 美食 > LeetCode 445.两数相加||

LeetCode 445.两数相加||

2024/12/22 11:21:02 来源:https://blog.csdn.net/m0_54244065/article/details/140373799  浏览:    关键词:LeetCode 445.两数相加||

1.题目要求:

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。示例1:输入:l1 = [7,2,4,3], l2 = [5,6,4]
输出:[7,8,0,7]
示例2:输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[8,0,7]
示例3:输入:l1 = [0], l2 = [0]
输出:[0]提示:链表的长度范围为 [1, 100]
0 <= node.val <= 9
输入数据保证链表代表的数字无前导 0

2.做题步骤:
1.遍历两个链表,得到它们的节点数:

 struct ListNode* cur1 = l1;struct ListNode* cur2 = l2;int count_1 = 0;int count_2 = 0;while(cur1){count_1++;cur1 = cur1->next;}while(cur2){count_2++;cur2 = cur2->next;}

2.然后创造两个数组,把两个链表的值放入两个数组中:

int* number1 = (int*)malloc(sizeof(int) * count_1);int* number2 = (int*)malloc(sizeof(int) * count_2);cur1 = l1;cur2 = l2;int i_1 = 0;int i_2 = 0;while(cur1){number1[i_1] = cur1->val;cur1 = cur1->next;i_1++;}while(cur2){number2[i_2] = cur2->val;i_2++;cur2 = cur2->next;}

3.然后再判断两个数组的长度的,把两个数组相加的值,放入长的数组:

if(count_1 > count_2){int i = 0;for(i = 0;i < count_1;i++){if(count_1 - i == count_2){break;}}int j = 0;for(j = 0;j < count_2;j++){number1[i] += number2[j];i++;}i = count_1 - 1;for(i = count_1 - 1;i > 0;i--){if(number1[i] > 9){number1[i - 1] += 1;number1[i] %= 10;}}if(number1[0] > 9){struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));head->next = l1;l1 = head;cur1 = l1;cur1->val = number1[0] / 10;cur1 = cur1->next;cur1->val = number1[0] % 10;cur1 = cur1->next;for(i = 1;i < count_1;i++){cur1->val = number1[i];cur1 = cur1->next;}return l1;}else{cur1 = l1;for(i = 0;i < count_1;i++){cur1->val = number1[i];cur1 = cur1->next;}return l1;}}else{int i = 0;for(i = 0;i < count_2;i++){if(count_2 - i == count_1){break;}}int j = 0;for(j = 0;j < count_1;j++){number2[i] += number1[j];i++;}i = count_2 - 1;for(i = count_2 - 1;i > 0;i--){if(number2[i] > 9){number2[i - 1] += 1;number2[i] %= 10;}}if(number2[0] > 9){struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));head->next = l2;l2 = head;cur2 = l2;cur2->val = number2[0] / 10;cur2 = cur2->next;cur2->val = number2[0] % 10;cur2 = cur2->next;for(i = 1;i < count_2;i++){cur2->val = number2[i];cur2 = cur2->next;}return l2;}else{cur2 = l2;for(i = 0;i < count_2;i++){cur2->val = number2[i];cur2 = cur2->next;}return l2;}}

全部代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){struct ListNode* cur1 = l1;struct ListNode* cur2 = l2;int count_1 = 0;int count_2 = 0;while(cur1){count_1++;cur1 = cur1->next;}while(cur2){count_2++;cur2 = cur2->next;}int* number1 = (int*)malloc(sizeof(int) * count_1);int* number2 = (int*)malloc(sizeof(int) * count_2);cur1 = l1;cur2 = l2;int i_1 = 0;int i_2 = 0;while(cur1){number1[i_1] = cur1->val;cur1 = cur1->next;i_1++;}while(cur2){number2[i_2] = cur2->val;i_2++;cur2 = cur2->next;}if(count_1 > count_2){int i = 0;for(i = 0;i < count_1;i++){if(count_1 - i == count_2){break;}}int j = 0;for(j = 0;j < count_2;j++){number1[i] += number2[j];i++;}i = count_1 - 1;for(i = count_1 - 1;i > 0;i--){if(number1[i] > 9){number1[i - 1] += 1;number1[i] %= 10;}}if(number1[0] > 9){struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));head->next = l1;l1 = head;cur1 = l1;cur1->val = number1[0] / 10;cur1 = cur1->next;cur1->val = number1[0] % 10;cur1 = cur1->next;for(i = 1;i < count_1;i++){cur1->val = number1[i];cur1 = cur1->next;}return l1;}else{cur1 = l1;for(i = 0;i < count_1;i++){cur1->val = number1[i];cur1 = cur1->next;}return l1;}}else{int i = 0;for(i = 0;i < count_2;i++){if(count_2 - i == count_1){break;}}int j = 0;for(j = 0;j < count_1;j++){number2[i] += number1[j];i++;}i = count_2 - 1;for(i = count_2 - 1;i > 0;i--){if(number2[i] > 9){number2[i - 1] += 1;number2[i] %= 10;}}if(number2[0] > 9){struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));head->next = l2;l2 = head;cur2 = l2;cur2->val = number2[0] / 10;cur2 = cur2->next;cur2->val = number2[0] % 10;cur2 = cur2->next;for(i = 1;i < count_2;i++){cur2->val = number2[i];cur2 = cur2->next;}return l2;}else{cur2 = l2;for(i = 0;i < count_2;i++){cur2->val = number2[i];cur2 = cur2->next;}return l2;}}
}

好了,这就是我的代码,大家如果觉得好的话,就给个免费的赞吧,谢谢各位大佬们了 ^ _ ^ .

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com