当前位置:网站首页>【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;
}
};
边栏推荐
- 云原生数据仓库AnalyticDB MySQL版用户手册
- Install a new version of idea. Double click it to open it
- re1攻防世界逆向
- Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
- POJ2392 SpaceElevator [DP]
- How to login and enable synchronization function in Google browser
- php 使用阿里云存储
- USB (XV) 2022-04-14
- 648. Word replacement
- As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
猜你喜欢
B_QuRT_User_Guide(38)
ROS2专题(03):ROS1和ROS2的区别【02】
leetcode-520. Detect capital letters -js
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Technology at home and abroad people "see" the future of audio and video technology
UE4_UE5全景相机
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
Add data analysis tools in Excel
Three questions TDM
伸展树(一) - 图文解析与C语言实现
随机推荐
leetcode-520. Detect capital letters -js
Inftnews | the wide application of NFT technology and its existing problems
[microservices SCG] gateway integration Sentinel
Matlab 信号处理【问答随笔·2】
Description of longitude and latitude PLT file format
ROS2专题(03):ROS1和ROS2的区别【01】
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
Vs extension tool notes
MySQL Index Optimization Practice I
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
V-for traversal object
Unity3d learning notes 5 - create sub mesh
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
FreeLink开源呼叫中心设计思想
Unity3D学习笔记5——创建子Mesh
Oracle database backup and recovery
Install Fedora under RedHat
LDO穩壓芯片-內部框圖及選型參數
Summary of SQL single table query 2020.7.27