当前位置:网站首页>LeetCode Algorithm 2181. 合并零之间的节点
LeetCode Algorithm 2181. 合并零之间的节点
2022-07-06 06:50:00 【Alex_996】
题目链接:2181. 合并零之间的节点
Ideas
算法:迭代
数据结构:链表
思路:其实可以新建一个链表res,然后通过head遍历原链表,遇到0节点时即创建一个新的节点挂载到res的尾部,直到遇到下一个0节点,否则就只让res的尾节点值加上当前正在遍历的节点。
Code
C++
class Solution {
public:
ListNode* mergeNodes(ListNode* head) {
ListNode *dummyHead = new ListNode(0);
ListNode *cur = dummyHead;
while (head->next != nullptr) {
if (head->val == 0) {
ListNode *node = new ListNode(0);
cur->next = node;
cur = node;
}
cur->val += head->val;
head = head->next;
}
return dummyHead->next;
}
};
边栏推荐
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- Pymongo gets a list of data
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- ROS2安装及基础知识介绍
- Office doc add in - Online CS
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- 基于购买行为数据对超市顾客进行市场细分(RFM模型)
- The difference between get and post request types
- 自动化测试环境配置
- Every API has its foundation when a building rises from the ground
猜你喜欢
How much is the price for the seal of the certificate
SQL Server Manager studio (SSMS) installation tutorial
My seven years with NLP
How to convert flv file to MP4 file? A simple solution
女生学软件测试难不难 入门门槛低,学起来还是比较简单的
基于PyTorch和Fast RCNN快速实现目标识别
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
18.多级页表与快表
18. Multi level page table and fast table
随机推荐
Day 239/300 注册密码长度为8~14个字母数字以及标点符号至少包含2种校验
[ 英语 ] 语法重塑 之 动词分类 —— 英语兔学习笔记(2)
Attributeerror successfully resolved: can only use cat accessor with a ‘category‘ dtype
Reflex WMS中阶系列3:显示已发货可换组
UDP攻击是什么意思?UDP攻击防范措施
The difference between get and post request types
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Day 248/300 thoughts on how graduates find jobs
LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
Fedora/rehl installation semanage
ROS learning_ Basics
How to convert flv file to MP4 file? A simple solution
PCL realizes frame selection and clipping point cloud
Simple use of MySQL database: add, delete, modify and query
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Delete external table source data
Chapter 7 - thread pool of shared model
Day 245/300 JS foreach data cannot be updated to the object after multi-layer nesting
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例