Ch2-4:add reverse 1 digit format number in linked list

frontend-design skills 立即下载

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

EXAMPLE

Input: (3 -> 1 -> 5), (5 -> 9 -> 2)

Output: 8 -> 0 -> 8

第一个想法是先把linkedlist变为int,相加,再初始化为一个linkedlist,但这样很耗时间:max(O(list1),O(list2))+O(listSUM)=O(listSUM).

第二种就是CC里面给的方法:因为recursive本质就是stack,而stack是LIFO,正好可以反过来,所以就用了这个方法:

node* addlist(node *list1, node * list2, int carry){
	node* result = new node();
	int value = carry;
	if(list1!=NULL)
		value += list1->data;
	if(list2!=NULL)
		value += list2->data;
	result->data = value % 10;
	if(list1!=NULL || list2!=NULL || value >= 10){
		node* more = addlist(list1!=NULL? list1->next: NULL, 
							 list2!=NULL? list2->next: NULL, 
							 value>=10? 1:0);
		result->next = more;
	}
	return result;
}

完整代码在这里。但是有个问题!

// 2-4 tony

#include 

using namespace std;

struct node{
    int data;
    node* next;
};

node* init(int* a, int n){
    node *head, *p;
	for (int i=0; idata = a[i];
		if(i==0){
			head = p = nd;
			continue;
		}
		p->next = nd;
		p = nd;
	}
    return head;
}

void print(node* head){
    if(head==NULL) {
        cout << "end" << endl;
        return;  // nust add return here so 
        			// as to jump out of recursion
    }
    cout<data<<" ";
    head=head->next;    
	print(head);
}

int linkedlist2int(node* head){
	int value = 0;
	if(head->next!=NULL){
		value = 10 * linkedlist2int(head->next);
	}
	return value+ head->data;
}

node* addlist(node *list1, node *list2, int carry){
	node* result = new node();
	int value = carry;
	if(list1!=NULL)
		value += list1->data;
	if(list2!=NULL)
		value += list2->data;
	result->data = value % 10;
	if(list1!=NULL || list2!=NULL || value >= 10){
		node* more = addlist(list1!=NULL? list1->next: NULL, 
							 list2!=NULL? list2->next: NULL, 
							 value>=10? 1:0);
		result->next = more;
	}
	return result;
}

int main(){
	int n = 3;
	int a[] = {
        //3, 0, 8
        1, 2, 9, 3
    };
    
    int b[] = {
        //4,9,2
        9,9,2
    };

    node* heada = init(a,4);
    print(heada);
    cout << linkedlist2int(heada)<

输出结果是:
Executing the program....
$demo 
1 2 9 3 end
3921
9 9 2 end
299
0 2 2 4 0 end  //这里应该输出0 2 2 4 end, 为什么多个0?
4220



车机破解避坑指南:为什么你的CarPlay盒子不识别?从USB设备ID解析兼容性问题 本文深入解析车机USB设备兼容性问题,特别是CarPlay盒子不识别的原因,从硬件ID到识别机制全面剖析。通过ADB工具和root权限操作,提供详细的破解方案和兼容性测试结果,帮助用户解决车机与第三方设备的连接问题。 阅读详情

相关推荐

Unity技能系统核心:从理论到实践构建可扩展的Buff管理器

本文深入探讨了Unity游戏开发中构建可扩展技能系统的核心——Buff管理器。通过剖析三层架构(数据、逻辑、渲染)与独立组件管理模式,提供了从Buff数据定义、管理器核心实现到可视化编辑器搭建的完整实践方案,旨在解决技能系统维护难题,提升开发效率与代码质量。

weixin_29002209的博客 256

anki制作灵活简洁的英语单词模板

anki自带的模板其实可以满足记单词的需求,但形式单一,用basic and reverse模板的话一个笔记只能产生2张卡片,如果一个单词有多个中文意思,那么我们在记忆的时候是看着所有的中文意思记忆一个单词,如果单独看不同的中文释义记忆是不是更好呢?

1周是1年的2% 1万+

射频电源与等离子体技术:原理、应用与优化策略

射频电源(RF Power Supply)作为高频交流电信号发生器,在半导体制造和材料处理中扮演关键角色。其核心原理是通过高频电场激发气体产生等离子体,这种电离气体包含电子、离子和活性基团,具有独特的非平衡特性。技术价值体现在等离子体密度和电子温度的可控性上,使得在刻蚀和沉积工艺中能精确调控材料处理效果。应用场景广泛覆盖PECVD、深槽刻蚀等工业流程,其中13.56MHz ISM频段因其全球合规性成为行业标准。优化策略包括双频驱动和阻抗匹配网络设计,可显著提升工艺均匀性和设备效率。

weixin_29163797的博客 395

anki卡片浏览器_Anki Guide by Corkine

Anki可以提醒我们按时复习知识,它也可以帮助我们保管学习之后的笔记,并将其制作成美观、易用的卡片,它的自定义程度相当的高,功能也十分丰富。它是一个很棒的软件,却几乎完全免费。1. 主要理论Anki主要有两大理论基础,它们分别是回顾复习和分散记忆。回顾复习 通过回顾学习过而又觉得生疏的内容,我们可以大大减少遗忘。对于回顾,最大的问题是,对于一个知识点的记忆,我们可能需要制作多张卡片,比如,我想要记...

weixin_35773740的博客 1690

导航系统中坐标系介绍

日常开发中常用到八大坐标系: 1. 惯性坐标系 圆心是地球的原点,Z轴沿地轴方向朝向北极, X轴和Y轴位于赤道平面内,与Z轴满足右手法则,并且X轴和Y轴分别指向两个恒星。也就是说不随着地球的自转而发生变化。它是一个非常固定的坐标系。IMU测量得到的加速度,角速度都是相对于这个坐标系的。 特点:xy不动,不随着地球的自转而转动,可以作为地球附近传感器输出的惯性坐标系。 在导航中,有: 载体在导航系下的角速度 = 载体相对惯性系的角速度 – 导航系相对于惯性系的角速度 2. 地球坐标系 地球坐标系用oxey

xiaojinger_123的博客 7738

Part03 数据结构

本文介绍了CSP-S初赛中常见的高级数据结构及其实现,包括: 线性结构:双端栈、双端队列、单调队列、优先队列和ST表,重点分析了它们的特点、时间复杂度及典型应用场景。 集合与森林:并查集(带路径压缩和按秩合并)和树的孩子兄弟表示法,展示了多叉树转化为二叉树存储的方法。 特殊树结构:包括二叉堆、树状数组、线段树和字典树,详细说明了它们的构建、查询和更新操作,特别强调了线段树的递归实现和字典树的前缀匹配特性。 每种数据结构都配有核心C++代码实现,涉及数组模拟、动态规划和树形结构等编程技巧,适合算法竞赛选手快速

weixin_43863957的博客 1181

leetcode -- 2026.6.30(6.26-6.30共10题)

数据结构出现题目核心考点你掌握的程度数组1, 4, 9双指针、哈希表优化、二分查找、中位数✅ 已拿下链表2虚拟头结点、进位处理、逆序加法✅ 已拿下字符串滑动窗口、中心扩展、Z字形模拟、状态机、递归/DP⚠️ 10需重点巩固数学/数字7, 9溢出判断、反转一半、回文判断(不转字符串)✅ 已拿下。

qq_59463944的博客 208

CANoe SOME/IP实战:从ARXML到VCODM的完整配置与调试

本文详细介绍了使用CANoe进行SOME/IP通信测试的完整流程,从硬件环境准备、ARXML文件处理到VCODM文件配置与调试。通过实战技巧和常见问题解决方案,帮助工程师高效完成从ARXML到VCODM的配置,确保SOME/IP通信的稳定性和可靠性。

weixin_29169899的博客 468

cc150做题计划

计划:12.29.2013 - 1.29.2013 做完第一遍题目,然后2月做leetcode。 2月底面试前完成200题C++。 这里记录一下ctci学习的参考资料。 1. hawstein的blog 2. https://github.com/ChengTian/CtCI 3. absolute C++ 5th 4. Primer C++ 5. learncpp:http

纯黑老白的专栏 2910

光学仿真案例(1) 铌酸锂非线性波导仿真模拟 FDTD仿真

在过去几年里,许多集成光子学应用往往需要多个相干可调谐光源,而且从外部激光器引入额外的光输入增加了芯片封装的复杂性,如果使用基于III-V族、氮化硅(SiN)或铌酸锂(LNO)的集成光源又大大增加了制造复杂性和成本。然而,通过非线性谐波相互作用产生的光源不需要额外的制造工艺来产生增益、反馈和外部电路,即可支持许多应用。在下面的案例中,演示了一种基于Luo[1]的铌酸锂波导频率转换集成系统。该案例主要仿真了利用温度调制实现高效相位匹配的铌酸锂纳米光子波导的二次谐波产生。

q895235273的博客 4148

工业级Modbus ASCII实时监控系统(WinForms完整实现)

本文介绍了一个基于WinForms的工业级Modbus ASCII实时监控系统实现方案。该系统采用经典工控界面布局,包含串口配置、数据采集控制和实时数据显示三大区域。核心功能包括:串口通信参数设置、Modbus从站地址配置、寄存器数据采集、实时数据表格展示和趋势图绘制。系统通过ModbusAsciiClient类实现通信核心,采用定时器轮询机制采集数据,支持最多100个数据点的历史趋势显示。界面设计注重用户体验,配置区位于左侧,主显示区在右侧,底部为状态和日志区,符合工业控制系统的操作习惯。代码示例展示了窗

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 279

ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods

In this problem, it is important to fully understand the definition of least common ancestor of 2 nodes: it means: (from leetcode blog: 点击打开链接) _______3______ / \

纯黑老白的专栏 1330

手把手教你学Simulink——基于经典PID控制的电机系统场景实例:BLDC电机PID电流控制策略仿真

*无刷直流电机(Brushless DC Motor, BLDC)**因其高效率、长寿命和低维护成本,在众多领域如家用电器、工业自动化、电动汽车等得到了广泛应用。为了实现对BLDC电机的有效控制,通常采用基于霍尔传感器或反电动势检测的换相技术,并结合电流闭环控制以提高系统的动态响应和稳定性。本教程将带你使用构建一个基于PID控制的BLDC电机电流控制策略仿真模型BLDC电机建模PID控制器设计与应用系统仿真与调试控制效果分析本文介绍了如何使用构建一个。

MHD0815的博客 198

Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)

这个有点像DSP FPGA Chang Choo的题目,一般是放大插值,DFT,IIR之类的Verilog题目。 题目在这:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees.

纯黑老白的专栏 1295

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Ch2-2: return the nth to the last node data of a singly linked list

In this problem, I need to find the nth to the last node data of a singly linked list.  In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack an

纯黑老白的专栏 1290

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

1/22/2014: 纪念第一个C++ programmer电面--F家

经过20天的CC刷题(其实是学习),才刚刚做到第四章。看看其他非ACM牛人,都是leetcode,CC,EPI滚瓜烂熟才去面。而且onsite确实是靠实力,要有做过大project之后才能真正融会贯通C++精神。 Anyway,谢谢郭志给的refer,不然连电面的机会都没有。 开头是C++基础题: 1. Fib的2种写法:recursive和DP 2. Struct和Class的区别;在C

纯黑老白的专栏 1274

Ch1-7: clear the row i and column j iff a[i][j]==0

in this problem, both monish001 and hawstein use O(n^2) solution which first tranverse the char** a to find the a[i][j]==0 and mark that row/col to be: TRUE. Then the send loop make the TURE row/col

纯黑老白的专栏 1207

复习一下dfs和bfs 用java

dfs 和 bfs都是用来找从source 到 target的path的算法。 dfs用的是recursion,bfs则用queue

纯黑老白的专栏 1191

Ch3-1: use a single array to implement three stacks.

Here it explains why Hawstein use this in his constructor, because he use size a parameter in his constructor while "size" is also a member variable(it's private), so he use this->size = size, explici

纯黑老白的专栏 1097

ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one

// Ch4.7: You have two very large binary trees: T1, with millions of nodes, and T2, // with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1 #include #include #includ

纯黑老白的专栏 1030

复习recursion

1. 如果这样 private void lop(int x, int size, double[] jerry){ if(x==size-1){ jerry[x] = -1; return; } if(jerry[x+1]==0){ jerry[x+1] = jerry[x]- 1; //if so, lop(x+1, size, jerry); }

纯黑老白的专栏 1009

Ch2-5: find the beginning of loop in a circular linked list---two solutions

Given a circular linked list, implement an algorithm which returns node at the beginning of the loop. DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer poin

纯黑老白的专栏 986

Ch3-2: implement min() for a stack with O(1)

Here will show the thinking flow from bad to good. idea 1: const int MAX_INT = ~(1<<31);//2147483647 struct node{ int val, min; }; class StackWithMin{ public: StackWithMin(int size){

纯黑老白的专栏 956

Ch8.8: 8 queen problem

In order to solve this problem efficiently, need to use backtracking method instead of iterations. Aka: Backtracking is a general algorithm for finding all (or some) solutions to some computational p

纯黑老白的专栏 949

Ch3-3: popat(int idx) of setofstack

Continue to use class to design solution, also, start to use STL, such as container (vector, stack, set, ...). save time and to be more professional. The design without popat method is similar to the

纯黑老白的专栏 948

Ch4-2: find if 2 given vertices are connected in a directed map,BFS + JUST fix the DFS recursion

In this problem, there are 2 ways: BFS or DFS. Hawstein used BFS: #include #include #include #include using namespace std; const int maxn = 100; bool g[maxn][maxn], visited[maxn]; int n;

纯黑老白的专栏 938
上一篇: Ch2-3: remove the middle node in a singly linked list
下一篇: Ch2-5: find the beginning of loop in a circular linked list---two solutions
纯黑老白
博客等级 码龄18年 30粉丝 62原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值