当前位置:网站首页>【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;
}
};
边栏推荐
- The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
- windows设置redis开启自动启动
- B / Qurt Utilisateur Guide (36)
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
- js 获取对象的key和value
- FreeLink开源呼叫中心设计思想
- B_QuRT_User_Guide(36)
猜你喜欢
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
进度播报|广州地铁七号线全线29台盾构机全部完成始发
RE1 attack and defense world reverse
Deep understanding of MySQL lock and transaction isolation level
Summary of SQL single table query 2020.7.27
Installing spss25
2021ICPC上海 H.Life is a Game Kruskal重构树
Lm12 rolling heikin Ashi double K-line filter
Talk about the design and implementation logic of payment process
B_QuRT_User_Guide(37)
随机推荐
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Install Fedora under RedHat
Add data analysis tools in Excel
Lm12 rolling heikin Ashi double K-line filter
Installing spss25
UE4_ Ue5 panoramic camera
Ros2 topic (03): the difference between ros1 and ros2 [01]
Description of longitude and latitude PLT file format
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
USB (XVII) 2022-04-15
USB (XVIII) 2022-04-17
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
欢聚时代一面
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
Archlinux install MySQL
系统架构设计师备考经验分享:论文出题方向
Three questions TDM
v-for遍历对象
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
Matlab SEIR infectious disease model prediction