当前位置:网站首页>【7.4】25. K 个一组翻转链表
【7.4】25. K 个一组翻转链表
2022-07-07 21:52:00 【howtoloveyou】
class Solution {
public:
//反转一个链表并返回头尾节点
pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail) {
ListNode* prev = tail->next;
ListNode* p = head;
while (prev != tail) {
ListNode* nex = p->next;
p->next = prev;
prev = p;
p = nex;
}
return {
tail, head};
}
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* hair = new ListNode(0);
hair->next = head;
ListNode* pre = hair;
while (head) {
ListNode* tail = pre;
for (int i = 0; i < k; ++i) {
tail = tail->next;
if (!tail) {
return hair->next;
}
}
ListNode* nex = tail->next;
tie(head, tail) = myReverse(head, tail); //进行翻转并将head,tail赋予新的值
pre->next = head; //将链表的头尾进行链接
tail->next = nex;
pre = tail;
head = tail->next;
}
return hair->next;
}
};
边栏推荐
- LDO稳压芯片-内部框图及选型参数
- Installing spss25
- 树后台数据存储(採用webmethod)[通俗易懂]
- VS扩展工具笔记
- Extended tree (I) - graphic analysis and C language implementation
- V-for traversal object
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- USB (XVII) 2022-04-15
- Add data analysis tools in Excel
- MySQL Index Optimization Practice II
猜你喜欢
2022第六季完美童模陕西总决赛圆满落幕
B_ QuRT_ User_ Guide(36)
Add data analysis tools in Excel
三问TDM
In the field of software engineering, we have been doing scientific research for ten years!
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
Deep understanding of MySQL lock and transaction isolation level
STL标准模板库(Standard Template Library)一周学习总结
随机推荐
Design and implementation of spark offline development framework
B_ QuRT_ User_ Guide(40)
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
MATLAB signal processing [Q & A essays · 2]
Matlab 信号处理【问答随笔·2】
Explain
进度播报|广州地铁七号线全线29台盾构机全部完成始发
Archlinux install MySQL
HDU 4747 Mex「建议收藏」
Sequence of entity layer, Dao layer, service layer and controller layer
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Install a new version of idea. Double click it to open it
Vs extension tool notes
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
Mysql索引优化实战一
MySQL Index Optimization Practice I
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
Count the top 10 films at the box office and save them in another file
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
UE4_UE5结合罗技手柄(F710)使用记录