当前位置:网站首页>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;
}
};
边栏推荐
- 国内手机厂商曾为它大打出手,如今它却最先垮台……
- STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
- [Cloud Native] 4.2 DevOps Lectures
- TF generates uniformly distributed tensor
- Usage of placeholder function in Tensorflow
- 阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
- Several ways to draw timeline diagrams
- 用户代码未处理MetadataException
- AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
- Simple questions peek into mathematics
猜你喜欢
Converting angles to radians
一本通2074:【21CSPJ普及组】分糖果(candy)
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
leetcode 38. 外观数列
JSON 基本使用
台风生成,广州公交站场积极开展台风防御安全隐患排查
电脑系统重装后怎么用打印机扫描出文件?
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
[Cloud Native] 4.2 DevOps Lectures
随机推荐
Deceptive Dice
FileZilla搭建FTP服务器图解教程
接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
AI识万物:从0搭建和部署手语识别系统
APP automation test framework - UiAutomator2 introductory
Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
Domestic mobile phone manufacturers once fought for it, but now it is the first to collapse...
README_Albumentations
STC8H开发(十五): GPIO驱动Ci24R1无线模块
How do task flow executors work?
Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
【软考 系统架构设计师】案例分析④ 软件架构风格
Basic JSON usage
BulkInsert方法实现批量导入
APP自动化测试框架-UiAutomator2基础入门
navicat 快捷键
Synchronization lock synchronized traces the source
JS解混淆-AST还原案例