当前位置:网站首页>【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 Collegiate Programming Contest VP记录+补题
- UE4_UE5全景相机
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- One week learning summary of STL Standard Template Library
- UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
- USB (XVII) 2022-04-15
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
- B_QuRT_User_Guide(36)
- Unity3D学习笔记5——创建子Mesh
- FreeLink开源呼叫中心设计思想
猜你喜欢

七月第一周

建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置

Deep understanding of MySQL lock and transaction isolation level

LDO voltage stabilizing chip - internal block diagram and selection parameters

Markdown

漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
![Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
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."

Explain

PCI-Express接口的PCB布线规则

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
随机推荐
LM12丨Rolling Heikin Ashi二重K线滤波器
re1攻防世界逆向
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
做自媒体视频剪辑怎么赚钱呢?
List. How to achieve ascending and descending sort() 2020.8.6
LeeCode -- 6. Zigzag transformation
UE4_ Ue5 panoramic camera
V-for traversal object
B_QuRT_User_Guide(40)
Matlab 信号处理【问答随笔·2】
Mysql索引优化实战二
USB (XVIII) 2022-04-17
2022第六季完美童模陕西总决赛圆满落幕
Summary of SQL single table query 2020.7.27
VS扩展工具笔记
MySQL Index Optimization Practice I
Unity3d learning notes 5 - create sub mesh
1. Sum of two numbers
【编译原理】词法分析设计实现
Lm12 rolling heikin Ashi double K-line filter