当前位置:网站首页>Leetcode.25 K个一组翻转链表(模拟/递归)
Leetcode.25 K个一组翻转链表(模拟/递归)
2022-08-09 21:54:00 【Curz酥】
题目链接:https://leetcode.cn/problems/reverse-nodes-in-k-group/
题目描述
给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
示例 1:
输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]
示例 2:
输入:head = [1,2,3,4,5], k = 3
输出:[3,2,1,4,5]
提示:
链表中的节点数目为 n
1 <= k <= n <= 5000
0 <= Node.val <= 1000
思路: 先定义一个反转链表的方法,递归调用即可。注意其中指针的使用,这是易错易混点。
C++代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* dummy = new ListNode(0); //创建一个哑结点
dummy->next = head; //哑结点指向原链表的头结点
ListNode* pre = dummy, *end = dummy;
while(end->next != NULL){
for(int i = 0; i < k and end != NULL; i++) end = end->next;
if(end == NULL) break;
ListNode* start = pre->next; //start指向要反转的部分的第一个结点
ListNode* next = end->next; //起到临时结点的作用,使得更好地链接链表
end->next = NULL; //为了反转链表,不然reverList里的while跳不出去
pre->next = reverseList(start); //递归反转链表
start->next = next; //每次反转完一部分,之前的指针也要跟上来
pre = start;
end = pre;
}
return dummy->next;
}
ListNode* reverseList(ListNode* head){ //迭代法反转链表
ListNode* cur = head, *pre = NULL;
while(cur){
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
边栏推荐
- UML类图五种关系的代码实现[通俗易懂]
- Technology Sharing | How to use the JSON Schema mode of interface automation testing?
- 【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
- STC8H开发(十五): GPIO驱动Ci24R1无线模块
- Use convert_to_tensor in Tensorflow to specify the type of data
- BulkInsert方法实现批量导入
- Sudoku | Backtrack-7
- TF uses constant to generate data
- 面试官:Redis 大 key 要如何处理?
- abstract class or interface
猜你喜欢
6 rules to sanitize your code
Jinshanyun earthquake, the epicenter is in bytes?
“稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
How do task flow executors work?
Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
Several ways to draw timeline diagrams
好未来,想成为第二个新东方
Activiti7审批流
你真的了解乐观锁和悲观锁吗?
随机推荐
孙正义亏掉1500亿:当初投贵了
蔚来杯2022牛客暑期多校训练营7 CFGJ
APP automation test framework - UiAutomator2 introductory
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
L3-2 Delete up to three characters (30 points)
Under the NVM node installation;The node environment variable configuration
Space not freed after TRUNCATE table
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
Bean life cycle
Leetcode 93 IP addresses
Easyui 表单验证「建议收藏」
unit test
Flask入门学习教程
Ehrlich screening method: Counting the number of prime numbers
2022 首期线下 Workshop!面向应用开发者们的数据应用体验日来了 | TiDB Workshop Day
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
SQLi-LABS Page-2 (Adv Injections)
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
电脑系统重装后怎么用打印机扫描出文件?