当前位置:网站首页>k个一组反转链表
k个一组反转链表
2022-06-30 20:39:00 【刀么克瑟拉莫】
一、递归
- 思路:
k_reverse取k个节点反转,剩下的再调用k_reverse取k个节点反转,直到节点数不等于k - 关键在于理解
part_reverse后的k个节点是end开头,head结尾,所以前k个节点的head指向后k个节点的end(已经反转的)
struct link
{
int a;
link* next;
};
link* part_reverse(link *begin, link *end) {
link *pre = end->next;
while(pre != end) {
link *tmp = begin->next;
begin->next = pre;
pre = begin;
begin = tmp;
}
return pre;
}
link* k_reverse(link* head, int k) {
int cnt = 0;
link * end = head;
while(end != NULL) {
++cnt;
if(cnt == k) break;
end = end->next;
}
if(cnt != k) return head;
link *tmp = part_reverse(head, end);
head->next = k_reverse(head->next, k);
return tmp;
}
二、循环
边栏推荐
猜你喜欢

Lumiprobe无铜点击化学解决方案

注册设备监理师难考吗,和监理工程师有什么关系?

软工UML画图

Basic components of STL

Study on PEGylation of lumiprobe and PEG linker - iodine-peg3-acid

Lumiprobe cell biology - dia, instructions for lipophilic tracer

Is the project manager a leader? Can you criticize and blame members?

浅谈代码语言的魅力

qiao-npms:搜索npm包

PHP obtains opcode and C source code
随机推荐
请指教在线开户需要什么银行卡?另外想问,现在在线开户安全么?
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
第81场双周赛
Lumiprobe无铜点击化学解决方案
B_QuRT_User_Guide(34)
Qt和其它GUI库的对比
PM reports work like this, and the boss is willing to give you a raise
北京大学ACM Problems 1001:Exponentiation
SQL必需掌握的100个重要知识点:创建和操纵表
【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总
报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
Informatics Olympiad 1362: family problems
Huffman Tree (1) Basic Concept and C - language Implementation
Qiao NPMS: search for NPM packages
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
修改已经上线的小程序名称
stacking集成模型预测回归问题
Basic concepts of tree
北京大学ACM Problems 1005:I Think I Need a Houseboat
SQL优化