当前位置:网站首页>链表相加(二)
链表相加(二)
2022-08-10 21:49:00 【干饭小白】

这题拿到手上,我一直在考虑一个问题就是,能不能通过不预先遍历的方法去做。
呜呜,原谅我的无知,我想不出来。
那就只能先遍历一遍了,反正也是很不影响时间。
一看到这题,就冒出了两种解题方法,第一种是预先通过两个数组把两个链表中的数据保存下来,然后就变成了大数加法了,然后再把结果放到一个新的链表中。
还一种是补充短的链表,然后进行各个位相加,最后考虑进位。等等,好像不可以吧,如果这样的话,进位没办法解决了,毕竟是一个单链表。如果可行,最后还是要借助数组完成进位。
两种方法我都试一下吧!!!
解法1:开始我想着顺着存,这样就可以在统计长度的时候把数据放入数组中.最后发现进位难处理


/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
int *arr1 = new int[1000002]; //用来保存结果
int *arr2 = new int[1000002];
ListNode* addInList(ListNode* head1, ListNode* head2) {
// write code here
//末尾对齐,遍历一遍
int len1 = 0;
int len2 = 0;
ListNode *p1 = head1;
ListNode *p2 = head2;
while(p1 != nullptr)
{
p1 = p1->next;
len1++;
}
while(p2 != nullptr)
{
p2 = p2->next;
len2++;
}
//将各个数位保存到数组中
p1 = head1;
for(int i=len1-1;i>=0;--i)
{
arr1[i] = p1->val;
p1 = p1->next;
}
p2 = head2;
for(int i=len2-1;i>=0;--i)
{
arr2[i] = p2->val;
p2 = p2->next;
}
//各个位相加
int len = len1 > len2 ? len1:len2;
for(int i=0;i<len;++i)
{
arr1[i] = arr1[i] + arr2[i];
arr1[i+1] = arr1[i+1] + arr1[i]/10;
arr1[i] = arr1[i]%10;
}
ListNode *ans = nullptr;
ListNode *p = nullptr;
if(arr1[len] > 0)
{
ListNode *s = new ListNode(arr1[len]);
s->next = nullptr;
ans = s;
p = s;
}
for(int k=len-1;k>=0;k--)
{
ListNode *s = new ListNode(arr1[k]);
s->next = nullptr;
if(ans == nullptr)
{
ans = s;
p = s;
}
else
{
p->next = s;
p = p->next;
}
}
return ans;
}
};这里好像忘记释放数组里的内存了,一个好的习惯,还是建议释放掉
解法2:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
ListNode* addInList(ListNode* head1, ListNode* head2) {
// write code here
//末尾对齐,遍历一遍
int len1 = 0;
int len2 = 0;
ListNode *p1 = head1;
ListNode *p2 = head2;
while(p1 != nullptr)
{
p1 = p1->next;
len1++;
}
while(p2 != nullptr)
{
p2 = p2->next;
len2++;
}
if(len1 > len2)
{
for(int i=0;i<len1-len2;++i)
{
ListNode *s = new ListNode(0);
s->next = head2;
head2 = s;
}
}
if(len2 > len1)
{
for(int i=0;i<len2-len1;++i)
{
ListNode *s = new ListNode(0);
s->next = head1;
head1 = s;
}
}
p1 = head1;
p2 = head2;
while(p1 != nullptr)
{
p1->val = p1->val + p2->val;
p1 = p1->next;
p2 = p2->next;
}
int len = len1 > len2 ? len1:len2;
int *arr = new int[len+2];
p1 = head1;
for(int i=len-1;i>=0;--i)
{
arr[i] = p1->val;
// arr[i+1] = arr[i+1] + arr[i]/10;
// arr[i] = arr[i]%10;
p1 = p1->next;
}
for(int i=0;i<len;++i)
{
arr[i+1] = arr[i+1] + arr[i]/10;
arr[i] = arr[i]%10;
}
ListNode *ans = nullptr;
ListNode *p = nullptr;
if(arr[len] > 0)
{
ListNode *s = new ListNode(arr[len]);
s->next = nullptr;
ans = s;
p = s;
}
for(int k=len-1;k>=0;--k)
{
ListNode *s = new ListNode(arr[k]);
s->next = nullptr;
if(ans == nullptr)
{
ans = s;
p = s;
}
else
{
p->next = s;
p = p->next;
}
}
delete []arr;
return ans;
}
};边栏推荐
- These must-know JVM knowledge, I have sorted it out with a mind map
- Thread State 详解
- labelme-屏蔽拖拽的事件
- labelme - block drag and drop events
- APP UI自动化测试常见面试题,或许有用呢~
- 2022.8.8 Selected Lectures on Good Topics (Number Theory Field)
- 自组织是管理者和成员的双向奔赴
- 什么是Jmeter?Jmeter使用的原理步骤是什么?
- 如何保护 LDAP 目录服务中的用户安全?
- 谁是边缘计算服务的采购者?是这六个关键角色
猜你喜欢

美创科技勒索病毒“零信任”防护和数据安全治理体系的探索实践

Service - DNS forward and reverse domain name resolution service

使用 Cloudreve 搭建私有云盘

【PCBA solution】Electronic grip strength tester solution she'ji

元宇宙社交应用,靠什么吸引用户「为爱发电」?

接口测试的概念、目的、流程、测试方法有哪些?

Shell编程之条件语句(二)

使用SylixOS虚拟串口,实现系统串口自由

LeetCode-36-Binary search tree and doubly linked list

camera preview process --- from HAL to OEM
随机推荐
mmpose关键点(一):评价指标(PCK,OKS,mAP)
ArcGIS自动随机生成采样点的方法
ThreadLocal comprehensive analysis (1)
shell (text printing tool awk)
shell programming without interaction
美味石井饭菜
How to secure users in LDAP directory service?
These must-know JVM knowledge, I have sorted it out with a mind map
【开源教程5】疯壳·开源编队无人机-飞控固件烧写
GAN CFOP
[Maui official version] Create a cross-platform Maui program, as well as the implementation and demonstration of dependency injection and MVVM two-way binding
谁是边缘计算服务的采购者?是这六个关键角色
从斐波那契 - 谈及动态规划 - 优化
【PCBA方案设计】蓝牙跳绳方案
什么是Jmeter?Jmeter使用的原理步骤是什么?
RADIUS Authentication Server Deployment Costs That Administrators Must Know
LeetCode-402 - Remove K digits
黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译
直播课堂系统08补-腾讯云对象存储和课程分类管理
The Thread State,