当前位置:网站首页>【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;
}
};
边栏推荐
- 系统设计概述
- Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
- Open source hardware small project: anxinco esp-c3f control ws2812
- Mysql索引优化实战二
- How to change the formula picture in the paper directly into the formula in word
- 2022 届的应届生都找到工作了吗?做自媒体可以吗?
- 648. Word replacement
- 谷歌浏览器怎么登录及开启同步功能
- 【7.4】25. K 个一组翻转链表
- Live-Server使用
猜你喜欢

伸展树(一) - 图文解析与C语言实现

Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting

B_QuRT_User_Guide(37)
![Ros2 topic (03): the difference between ros1 and ros2 [01]](/img/20/39d47c93400050a7bc8ad7efea51b3.png)
Ros2 topic (03): the difference between ros1 and ros2 [01]

三问TDM
![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."

MySQL Index Optimization Practice II

Live server usage
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」

leetcode-520. Detect capital letters -js
随机推荐
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
LeeCode -- 6. Zigzag transformation
POJ2392 SpaceElevator [DP]
B / Qurt Utilisateur Guide (36)
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."
Summary of SQL single table query 2020.7.27
How to change the formula picture in the paper directly into the formula in word
Cloud native is devouring everything. How should developers deal with it?
HDU 4747 mex "recommended collection"
Boost regex library source code compilation
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
SAP HR奖罚信息导出
Spark 离线开发框架设计与实现
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
Freelink open source call center design idea
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
JNI uses asan to check memory leaks