当前位置:网站首页>LeetCode Algorithm 2181. Merge nodes between zero
LeetCode Algorithm 2181. Merge nodes between zero
2022-07-06 07:06:00 【Alex_ 12 hours a day 6 days a week】
Topic link :2181. Merge nodes between zeros
Ideas
Algorithm : iteration
data structure : Linked list
Ideas : In fact, you can create a new linked list res, And then through head Traverse the original list , encounter 0 Create a new node and mount it to res Tail of , Until we meet the next one 0 node , Otherwise, just let res The tail node value of plus the node currently being traversed .
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实现选框裁剪点云
- 《从0到1:CTFer成长之路》书籍配套题目(周更)
- Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
- Zhongqing reading news
- Cif10 actual combat (resnet18)
- 19. Actual memory management of segment page combination
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- 首发织梦百度推送插件全自动收录优化seo收录模块
- Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
- A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
猜你喜欢
首发织梦百度推送插件全自动收录优化seo收录模块
Development of entity developer database application
leetcode59. 螺旋矩阵 II(中等)
AI on the cloud makes earth science research easier
19. Actual memory management of segment page combination
漏了监控:Zabbix对Eureka instance状态监控
这个高颜值的开源第三方网易云音乐播放器你值得拥有
ROS learning_ Basics
Establishment and operation of cloud platform open source project environment
Introduction to ros2 installation and basic knowledge
随机推荐
The author is dead? AI is conquering mankind with art
Windows Server 2016 standard installing Oracle
顶测分享:想转行,这些问题一定要考虑清楚!
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Arduino tutorial - Simon games
将ue4程序嵌入qt界面显示
WPF之MVVM
Interface automation test framework: pytest+allure+excel
Cookie技术&Session技术&ServletContext对象
Leetcode 78: subset
【Hot100】739. 每日温度
Introduction to ros2 installation and basic knowledge
从autojs到冰狐智能辅助的心里历程
首发织梦百度推送插件全自动收录优化seo收录模块
18.多级页表与快表
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
作者已死?AI正用艺术征服人类
这个高颜值的开源第三方网易云音乐播放器你值得拥有