当前位置:网站首页>leetcode25题:K 个一组翻转链表——链表困难题目详解
leetcode25题:K 个一组翻转链表——链表困难题目详解
2022-07-27 14:36:00 【瘦皮猴117】
目录
题目
给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
提示:
链表中的节点数目为 n
1 <= k <= n <= 5000
0 <= Node.val <= 1000进阶:你可以设计一个只用 O(1) 额外内存空间的算法解决此问题吗?
分析
1. 判断链表是否为空、是否只有一个节点,是则返回空链表或该节点。
2. 统计链表节点个数,得出链表长度 length 。每组子链表反转的节点数 k ,可计算出应反转子链表的组数 num = length / k。
3. 要满足空间复杂度为O(1),需要在原链表上操作。使用头插法将每组子链表反转。设置计数器 count,控制反转组数为 num次。
代码实现
public class Num25_reverseKGroup { public ListNode reverseKGroup(ListNode head, int k) { //判空 if (head == null || head.next == null) { return head; } //此时链表至少有两个节点 ListNode x = head; int length = 0; while (x != null) { //统计链表的长度 length++; x = x.next; } int num = length / k; //计算需要反转的组数 int count = 0; //设置计数器,控制反转的组数 ListNode dummyHead = new ListNode(); dummyHead.next = head; ListNode prev = dummyHead; ListNode cur = prev.next; ListNode next = new ListNode(); while (count < num) { for (int i = 0; i < k - 1; i++) { //头插法反转链表,一次循环,反转一组 next = cur.next; cur.next = next.next; next.next = prev.next; prev.next = next; } prev = cur; cur = prev.next; count++; } return dummyHead.next; } }
时间复杂度:O(n) 最坏情况下,整个链表的每个节点分别会被遍历 1 次、翻转一次。
空间复杂度:O(1) 除了必须的节点指针外,并没有占额外空间。
如有Bug,望指正,感谢浏览 !
边栏推荐
- /Dev/loop1 takes up 100% of the problem
- 【云享读书会第13期】音频文件的封装格式和编码格式
- Implement custom spark optimization rules
- QT (IV) mixed development using code and UI files
- Division of entity classes (VO, do, dto)
- [tensorboard] oserror: [errno 22] invalid argument processing
- IP protocol of network layer
- Static关键字的三种用法
- 直接插入排序
- 线程中死锁的成因及解决方案
猜你喜欢

【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版

Network principle (1) - overview of basic principles

What format is this data returned from the background

C language: string function and memory function

Jump to the specified position when video continues playing

低代码是开发的未来吗?浅谈低代码平台

【剑指offer】面试题41:数据流中的中位数——大、小堆实现

Spark Bucket Table Join

MLX90640 红外热成像仪测温传感器模块开发笔记(七)

Static关键字的三种用法
随机推荐
[正则表达式] 单个字符匹配
Half find
Go language slow start - package
Spark troubleshooting finishing
网络原理(2)——网络开发
【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
C: What is the return value (revolution) in a function
Voice live broadcast system -- a necessary means to improve the security of cloud storage
Network principle (2) -- network development
JS operation DOM node
Use deconstruction to exchange the values of two variables
[sword finger offer] interview question 54: the k-largest node of the binary search tree
Push down of spark filter operator on parquet file
C语言中交换两数的方法
突破软硬壁垒,赛灵思面向开发者发布Vitis统一软件平台
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
[正则表达式] 匹配多个字符
Under the ban, the Countermeasures of security giants Haikang and Dahua!
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
QT (XIII) qchart drawing line chart


