当前位置:网站首页>【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;
}
};
边栏推荐
- 2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
- 【7.5】15. 三数之和
- SAP HR 劳动合同信息 0016
- USB (XVI) 2022-04-28
- MySQL Index Optimization Practice I
- LDO穩壓芯片-內部框圖及選型參數
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- How to login and enable synchronization function in Google browser
- Map operation execution process
- 进度播报|广州地铁七号线全线29台盾构机全部完成始发
猜你喜欢
Markdown
LeeCode -- 6. Z 字形变换
0-1背包问题
Three questions TDM
RE1 attack and defense world reverse
Mysql索引优化实战一
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
B / Qurt Utilisateur Guide (36)
Markdown
Ros2 topic (03): the difference between ros1 and ros2 [02]
随机推荐
Live-Server使用
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
8.31 Tencent interview
v-for遍历对象
SAP HR 家庭成员信息
POJ2392 SpaceElevator [DP]
Flash encryption process and implementation of esp32
Unity3d Learning Notes 6 - GPU instantiation (1)
Fibonacci number of dynamic programming
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
ROS2专题(03):ROS1和ROS2的区别【02】
JNI uses asan to check memory leaks
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
系统设计概述
PCB wiring rules of PCI Express interface
USB (XVIII) 2022-04-17
SAP 内存参数调优过程
Description of longitude and latitude PLT file format
Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
leetcode-520. Detect capital letters -js