当前位置:网站首页>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;
}
};
边栏推荐
- PCL实现选框裁剪点云
- 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
- On the first day of clock in, click to open a surprise, and the switch statement is explained in detail
- Day 245/300 JS forEach 多层嵌套后数据无法更新到对象中
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- SAP SD发货流程中托盘的管理
- 一文读懂简单查询代价估算
- What are the commonly used English words and sentences about COVID-19?
- Py06 dictionary mapping dictionary nested key does not exist test key sorting
- 万丈高楼平地起,每个API皆根基
猜你喜欢
我的创作纪念日
CS passed (cdn+ certificate) PowerShell online detailed version
mysql的基础命令
【每日一题】729. 我的日程安排表 I
雲上有AI,讓地球科學研究更省力
Reflex WMS中阶系列3:显示已发货可换组
Development of entity developer database application
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Database basics exercise part 2
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
随机推荐
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
医疗软件检测机构怎么找,一航软件测评是专家
Reflex WMS中阶系列3:显示已发货可换组
Leetcode daily question (1870. minimum speed to arrive on time)
《从0到1:CTFer成长之路》书籍配套题目(周更)
[daily question] 729 My schedule I
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
How much is the price for the seal of the certificate
RichView TRVStyle 模板样式的设置与使用
ROS learning_ Basics
pymongo获取一列数据
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
My creation anniversary
LeetCode - 152 乘积最大子数组
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
What are the commonly used English words and sentences about COVID-19?
CS passed (cdn+ certificate) PowerShell online detailed version
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
顶测分享:想转行,这些问题一定要考虑清楚!
Leetcode daily question (1997. first day where you have been in all the rooms)