当前位置:网站首页>【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;
}
};
边栏推荐
- Caip2021 preliminary VP
- 城联优品作为新力量初注入,相关上市公司股价应声上涨150%
- B_ QuRT_ User_ Guide(37)
- Force deduction solution summary 648 word replacement
- 1. Sum of two numbers
- USB (XV) 2022-04-14
- Design and implementation of spark offline development framework
- Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
- Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
- SQL database execution problems
猜你喜欢
Ros2 topic (03): the difference between ros1 and ros2 [02]
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
深入理解Mysql锁与事务隔离级别
UE4_ Ue5 combined with Logitech handle (F710) use record
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
LDO穩壓芯片-內部框圖及選型參數
MySQL Index Optimization Practice I
leetcode-520. Detect capital letters -js
PCI-Express接口的PCB布线规则
ESP at installation esp8266 and esp32 versions
随机推荐
V-for traversal object
[compilation principle] lexical analysis design and Implementation
SAP HR 家庭成员信息
JNI uses asan to check memory leaks
IDEA 2021.3. X cracking
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
2022第六季完美童模陕西总决赛圆满落幕
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
Boost regex library source code compilation
JS get the key and value of the object
Caip2021 preliminary VP
SAP 内存参数调优过程
Force deduction solution summary 648 word replacement
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
SLAM面试总结
sql 数据库执行问题
How can we make money by making video clips from our media?
Windows set redis to start automatically
Unity3d learning notes 4 - create mesh advanced interface