(牛客网)链表相加(二)

news/2025/1/19 1:35:45/

 嗯哼~



题目

描述

假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。

给定两个这种链表,请生成代表两个整数相加值的结果链表。

数据范围:0 ≤ n,m ≤ 1000000,链表任意值 0 ≤ val ≤ 9
要求:空间复杂度 O(n),时间复杂度 O(n)

示例 



思路 

 

单链表的翻转详细讲解:反转一个单链表(<---点击可看详解)



 

题解代码

struct ListNode* ReverseList(struct ListNode* pHead)
{// write code herestruct ListNode* cur = pHead;struct ListNode* pre = NULL;while (cur != NULL){struct ListNode* temp = cur->next;cur->next = pre;pre = cur;cur = temp;}return pre;
}struct ListNode* addInList(struct ListNode* head1, struct ListNode* head2)
{// write code herehead1 = ReverseList(head1);head2 = ReverseList(head2);int temp = 0;int add = 0;struct ListNode* newhead = NULL;while (head1 && head2){temp = (head1->val + head2->val + add);if (temp >= 10){add = 1;temp %= 10;}else{add = 0;}struct ListNode* cur = (struct ListNode*)malloc(sizeof(struct ListNode));cur->val = temp;cur->next = newhead;newhead = cur;head1 = head1->next;head2 = head2->next;}struct ListNode* empty = head1;struct ListNode* nonempty = head2;if (head1 != NULL){empty = head2;nonempty = head1;}while (nonempty){temp = (nonempty->val + add);if (temp >= 10){add = 1;temp %= 10;}else{add = 0;}struct ListNode* cur = (struct ListNode*)malloc(sizeof(struct ListNode));cur->val = temp;cur->next = newhead;newhead = cur;nonempty = nonempty->next;}if (add == 1){struct ListNode* cur = (struct ListNode*)malloc(sizeof(struct ListNode));cur->val = 1;cur->next = newhead;newhead = cur;}return newhead;}


 个人主页:Lei宝啊

愿所有美好如期而遇


http://www.ppmy.cn/news/1046239.html

相关文章

leetcode 198. 打家劫舍

2023.8.19 打劫问题是经典的动态规划问题。先设一个dp数组&#xff0c;dp[i]的含义为&#xff1a;前 i 个房屋能盗取的最高金额。 每间房屋无非就是偷&#xff0c;或者不偷这两种情况&#xff0c;于是可以写出递推公式&#xff1a; …

市值缩水80%,从光学到声学,瑞声科技押宝汽车赛道?

进入智能汽车赛道&#xff0c;并非上市公司的「良药」。通过海外收购的走捷径模式&#xff0c;也并非「及时雨」。 本周&#xff0c;评级机构穆迪宣布&#xff0c;确认瑞声科技(02018.HK)的评级从「稳定」调整为「负面」。穆迪表示&#xff0c;瑞声科技拟收购Premium Sounds S…

docker容器的安装以及基础命令操作

摘要&#xff1a;Docker 是一个开源的应用容器引擎&#xff0c;可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在本地编译测试通过的容器可以批量地在生产环境中部署&#xff0c;包括VMs&#xff08;虚拟机&#xff09;、baremetal、OpenStack 集群和…

【Linux C】在sprintf中打印双引号

0x00 前言 最后更新日期&#xff1a;2023.8.16 0x01 在sprintf中打印双引号 在字符串中有双引号”的地方前面加上一个反斜杠“\”即可&#xff0c;例如&#xff1a; char szProQuery[256] {0}; char name[256] "XiaoMing"; sprintf(szProQuery,"%s said :…

【环境配置】ubuntu2204-cudnn GPG key 错误

问题: cudnn GPG error Reading package lists... Done W: GPG error: file:/var/cudnn-local-repo-ubuntu2204-8.8.0.121 InRelease: …

go语言中channel类型

目录 什么是channel 为什么要有channel channel操作使用 初始化&#xff1a; 操作&#xff1a; 单向channel 双向channel&#xff0c;可读可写&#xff1a; close下什么场景会出现panic 总结 什么是channel Channels are a typed conduit through which you can send …

open cv学习 (十一)视频处理

视频处理 demo1 import cv2 # 打开笔记本内置摄像头 capture cv2.VideoCapture(0) # 笔记本内置摄像头被打开 while capture.isOpened():# 从摄像头中实时读取视频retval, image capture.read()# 在窗口中实时显示读取到的视频cv2.imshow("Video", image)# 等到用…

以创新点亮前路,戴尔科技开辟数实融合新格局

编辑&#xff1a;阿冒 设计&#xff1a;沐由 2023年&#xff0c;对于戴尔科技而言是特殊的一年&#xff0c;这是戴尔科技进入中国市场第25个年头——“巧合”的是&#xff0c;这25年也是中国产业经济发展最快&#xff0c;人们工作与生活发生变化最大的四分之一个世纪。 2023年&…