当前位置:网站首页>【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;
}
};
边栏推荐
- 统计电影票房排名前10的电影并存入还有一个文件
- Installing spss25
- 包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
- Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
- Conversion between commonsmultipartfile and file
- 2022第六季完美童模陕西总决赛圆满落幕
- Technology at home and abroad people "see" the future of audio and video technology
- 【编译原理】词法分析设计实现
- 力扣解法汇总648-单词替换
- USB(十四)2022-04-12
猜你喜欢
Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."
Add data analysis tools in Excel
产业共融新势能,城链科技数字峰会厦门站成功举办
STL标准模板库(Standard Template Library)一周学习总结
B_ QuRT_ User_ Guide(37)
【编译原理】词法分析设计实现
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
USB (XV) 2022-04-14
Cloud native is devouring everything. How should developers deal with it?
UE4_UE5结合罗技手柄(F710)使用记录
随机推荐
B_QuRT_User_Guide(40)
HDU 4747 Mex「建议收藏」
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
CAIP2021 初赛VP
STL标准模板库(Standard Template Library)一周学习总结
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
13、 System optimization
Cloud native data warehouse analyticdb MySQL user manual
2022第六季完美童模陕西总决赛圆满落幕
2021ICPC上海 H.Life is a Game Kruskal重构树
USB (XV) 2022-04-14
Ros2 topic (03): the difference between ros1 and ros2 [01]
Count the top 10 films at the box office and save them in another file
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
How can we make money by making video clips from our media?
POJ2392 SpaceElevator [DP]
B_QuRT_User_Guide(36)
LDO稳压芯片-内部框图及选型参数
Install a new version of idea. Double click it to open it