当前位置:网站首页>【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;
}
};
边栏推荐
- [stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
- Cloud native is devouring everything. How should developers deal with it?
- 生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
- 谷歌浏览器怎么登录及开启同步功能
- Have all the fresh students of 2022 found jobs? Is it OK to be we media?
- Spark 离线开发框架设计与实现
- PCB wiring rules of PCI Express interface
- Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
- Lm12 rolling heikin Ashi double K-line filter
- Happy gathering time
猜你喜欢
Open source hardware small project: anxinco esp-c3f control ws2812
Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
ROS2专题(03):ROS1和ROS2的区别【01】
Ros2 topic (03): the difference between ros1 and ros2 [01]
2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
ROS2专题(03):ROS1和ROS2的区别【02】
LDO稳压芯片-内部框图及选型参数
SAP 内存参数调优过程
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
Mysql索引优化实战一
随机推荐
FPGA basics catalog
StringUtils工具类
Freelink open source call center design idea
SAP HR 社会工作经历 0023
ESP at installation esp8266 and esp32 versions
POJ2392 SpaceElevator [DP]
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
【7.5】15. 三数之和
Caip2021 preliminary VP
Mysql索引优化实战一
The for loop realizes 1-100 addition and eliminates the 4-digit tail number
进度播报|广州地铁七号线全线29台盾构机全部完成始发
Unity3d learning notes 4 - create mesh advanced interface
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
【7.4】25. K 个一组翻转链表
Oracle database backup and recovery
How to change the formula picture in the paper directly into the formula in word
In the field of software engineering, we have been doing scientific research for ten years!