当前位置:网站首页>【7.4】25. Turn over the linked list in groups of K
【7.4】25. Turn over the linked list in groups of K
2022-07-07 23:34:00 【howtoloveyou】
class Solution {
public:
// Reverse a linked list and return the head and tail nodes
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); // Flip and head,tail Assign a new value
pre->next = head; // Link the head and tail of the linked list
tail->next = nex;
pre = tail;
head = tail->next;
}
return hair->next;
}
};
边栏推荐
- 系统设计概述
- 家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
- Unity3d Learning Notes 6 - GPU instantiation (1)
- Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
- Coreseek: the second step is index building and testing
- 648. Word replacement
- B_ QuRT_ User_ Guide(37)
- [stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
- One week learning summary of STL Standard Template Library
- How to change the formula picture in the paper directly into the formula in word
猜你喜欢

ROS2专题(03):ROS1和ROS2的区别【02】

Unity3d learning notes 5 - create sub mesh

Deep understanding of MySQL lock and transaction isolation level

How to change the formula picture in the paper directly into the formula in word
![Ros2 topic (03): the difference between ros1 and ros2 [01]](/img/20/39d47c93400050a7bc8ad7efea51b3.png)
Ros2 topic (03): the difference between ros1 and ros2 [01]

LM12丨Rolling Heikin Ashi二重K线滤波器

0-1背包问题

Map operation execution process

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

在软件工程领域,搞科研的这十年!
随机推荐
B_QuRT_User_Guide(36)
MySQL Index Optimization Practice I
做自媒体视频剪辑怎么赚钱呢?
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
Markdown
MATLAB signal processing [Q & A essays · 2]
Summary of common methods of object class (September 14, 2020)
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
B_QuRT_User_Guide(39)
Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
IDEA 2021.3. X cracking
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
Dynamic agent explanation (July 16, 2020)
POJ2392 SpaceElevator [DP]
Right click the idea file to create new. There is no solution to create new servlet
USB (XIV) 2022-04-12
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行