当前位置:网站首页>【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;
}
};
边栏推荐
- Inftnews | the wide application of NFT technology and its existing problems
- Matlab-SEIR传染病模型预测
- 云原生正在吞噬一切,开发者该如何应对?
- UE4_UE5结合罗技手柄(F710)使用记录
- 深入理解Mysql锁与事务隔离级别
- The 19th Zhejiang Provincial Collegiate Programming Contest 2022浙江省赛 F.EasyFix 主席树
- Entity层、DAO层、Service层、Controller层 先后顺序
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
- 2021ICPC上海 H.Life is a Game Kruskal重构树
- v-for遍历对象
猜你喜欢
随机推荐
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
JS get the key and value of the object
[compilation principle] lexical analysis design and Implementation
B_ QuRT_ User_ Guide(37)
MySQL Index Optimization Practice I
统计电影票房排名前10的电影并存入还有一个文件
2022第六季完美童模陕西总决赛圆满落幕
Mysql索引优化实战二
Happy gathering time
Tree background data storage (using webmethod) [easy to understand]
Extended tree (I) - graphic analysis and C language implementation
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
MATLAB signal processing [Q & A essays · 2]
UE4_ Ue5 combined with Logitech handle (F710) use record
经纬度PLT文件格式说明
产业共融新势能,城链科技数字峰会厦门站成功举办
Matlab 信号处理【问答随笔·2】
Oracle database backup and recovery
re1攻防世界逆向
Cloud native is devouring everything. How should developers deal with it?









