当前位置:网站首页>LeetCode 25:K个一组翻转链表
LeetCode 25:K个一组翻转链表
2022-06-21 16:02:00 【二芒星】
题目
给你一个链表,每 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]
示例3
输入:head = [1,2,3,4,5], k = 1
输出:[1,2,3,4,5]
示例4
输入:head = [1], k = 1
输出:[1]
思路
主要分为两步:
- 首先判断节点是否够k个,若不到k个直接返回(break)
- k个节点翻转:
- 如
0->1->2->3->4,假设k=3,翻转1->2->3,先让该k节点内部翻转1<-2<-3然后让0->3,最后再让1->4,从而形成0->3->2->1->4,形成翻转
- 如
时间复杂度分析:O(n),对链表进行遍历一次O(n),还有两次遍历都是常量空间O(k)
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) {
auto dummy = new ListNode(-1);
dummy->next = head;
for (auto p = dummy; ; ) {
auto q = p;
/** 判断该点之后是否有k个元素 */
for (int i = 0; i < k && q; i ++) q = q->next;
if (!q) break; /* 不足k个元素直接返回 */
auto a = p->next, b = a->next;
for (int i = 0; i < k - 1; i ++) {
auto c = b->next;
b->next = a;
a = b, b = c;
}
auto c = p->next;
p->next = a, c->next = b;
p = c;
}
return dummy->next;
}
};
边栏推荐
- The release of autok3s v0.5.0 continues to be simple and friendly
- 南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记
- Elegant request retry using guzzle Middleware
- [从零开始学习FPGA编程-38]:进阶篇 -语法-函数与任务
- Generating test reports using the unittest framework
- Résolution des erreurs signalées par qtcreator
- 既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
- 【SQLite】解决unrecognized token:“‘“
- I do 3D restoration for the aircraft carrier: these three details are shocking
- 海外new things | 美国人工智能初创「Zoovu」新一轮融资1.69亿美元,为消费者优化线上的“产品发现”体验
猜你喜欢

Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers

很多软件公司,其实都是“笑话”

The Google play academy team PK competition officially begins!

快来围观–TPT18新版报到

Remote connection raspberry pie in VNC Viewer Mode

还在用 Xshell ?试试这款炫酷的 SSH 终端工具吧,功能很强大!

Previous installation records

Yaml文件详解

Generating test reports using the unittest framework

Yaml数据驱动演示
随机推荐
Fidder tool usage notes
设计一个打印整棵树的打印函数
【SQLite】解决unrecognized token:“‘“
PowerPoint tutorial, how to change page orientation and slide size in PowerPoint?
Notice on Revising the guidelines for the planning, design and livable construction of housing with common property rights in Beijing (for Trial Implementation)
招募令|数据可视化开发平台“FlyFish”「超级体验官」招募啦!
exness:美国通货膨胀影响太大,美联储大佬纷纷表态
快速排序简单思路及程序
Use picgo core and Alibaba cloud to automatically upload typera pictures
Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022
实战---商场登录测试
Elegant request retry using guzzle Middleware
容器云是什么意思?与堡垒机有什么区别?
机器学习中的概念漂移(Aporia)
Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers
Calculation of carbon emissions
Pytest framework
二叉树的层序遍历
Move Protocol Beta测试版再调整,扩大总奖池
4. 构造【LR(1)分析表(包含构建项目规范族)】