当前位置:网站首页>BM3 flips the nodes in the linked list in groups of k
BM3 flips the nodes in the linked list in groups of k
2022-07-31 19:57:00 【Xiaoyu who loves programming】
BM3链表中的节点每k个一组翻转
最近在刷牛客上的编程题,记录一下~
描述
将给出的链表中的节点每 k 个一组翻转,返回翻转后的链表
如果链表中的节点数不是 k 的倍数,将最后剩下的节点保持原样
你不能更改节点中的值,只能更改节点本身.
数据范围:0≤n≤2000 ,1≤k≤2000,链表中每个元素都满足 0≤val≤1000
要求空间复杂度 O(1),时间复杂度 O(n)
例如:
给定的链表是 1→2→3→4→5
对于 k=2 , 你应该返回 2→1→4→3→5
对于 k=3 , 你应该返回 3→2→1→4→5
示例1
输入:
{1,2,3,4,5},2
复制
返回值:
{2,1,4,3,5}
复制
示例2
输入:
{},1
复制
返回值:
{}
解决思路:
It is roughly divided into the following three steps:
First group the linked list
Flip within groups
The flipped connection
**难点:**翻转后,每一组The head node of the original linked list corresponds to the flipped tail node,The tail node of the original linked list is the flipped head node,If we process each packet sequentially,It can be cumbersome to not create a new linked list.
解决办法:如果我们采用递归的方式Start flipping from the last group,得到了最后一个组的链表首,是不是可以直接连在倒数第二个组翻转后的尾(即翻转前的头)后面.
如果这个链表有n个分组可以反转,我们首先对第一个分组反转,那么是不是接下来将剩余n−1个分组反转后的结果接在第一组后面就行了,那这剩余的n−1组就是一个子问题.我们来看看递归的三段式模版:
- 终止条件: 当进行到最后一个分组,即不足k次遍历到链表尾(0次也算),就将剩余的部分直接返回.
- 返回值: Each level to return isThe flipped header of this packet,以及Concatenate all the flipped grouped linked lists behind it.
- 本级任务: 对于每个子问题,先遍历k次,找到该组结尾在哪里,然后从这一组开头遍历到结尾,依次翻转,结尾就可以作为下一个分组的开头,而The element that previously pointed to the beginning has gone to the end of the group,可以Use it to connect the sub-questions behind it,即The header of the following packet.
具体做法:
- step 1:每次从进入函数的头节点优先遍历链表k次,分出一组,若是后续不足k个节点,不用反转直接返回头.
- step 2:从进入函数的头节点开始,依次反转接下来的一组链表,The reversal process is the same as a normal flipped linked list
- step 3:这一组经过反转后,原来的头变成了尾,后面接下一组的反转结果,下一组采用上述递归继续.
图示:
import java.util.*;
/* * public class ListNode { * int val; * ListNode next = null; * } */
public class Solution {
/** * * @param head ListNode类 * @param k int整型 * @return ListNode类 */
public ListNode reverseKGroup (ListNode head, int k) {
// write code here
// Declare the tail of each set of nodes to be flipped,作为headThe parameters are passed to the next group for use
ListNode tail = head;
// 遍历ktimes to find the tail
for(int i = 1; i <= k; i++){
// 如果为空,Explain the remaining nodes and deficienciesK个
if(tail == null)
return head;
tail = tail.next;
}
// Flip the current group of nodes
ListNode pre = null;
ListNode temp = null;
ListNode cur = head;
while(cur != tail){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
// 现在的headThe pointer is already the tail node of each group
// Just change the current groupheadA pointer to the return value of the next group(The tail node of the original linked list,反转后的头节点)
head.next = reverseKGroup(tail, k);
// Returns the head pointer of the linked list after the current group is reversed(即原链表的尾节点)
return pre;
}
}
边栏推荐
- spark报错OutOfMemory「建议收藏」
- 多线程之锁
- iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
ojdbc8 "Recommended Collection"- 基于STM32 环形队列来实现串口接收数据
- Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
- Bika LIMS open source LIMS set - use of SENAITE (detection process)
- BM5 merge k sorted linked lists
- 基于WPF重复造轮子,写一款数据库文档管理工具(一)
- 返回一个零长度的数组或者空的集合,不要返回null
猜你喜欢
PCB stackup design
Architect 04 - Application Service Encryption Design and Practice
利用反射实现一个管理对象信息的简单框架
Getting Started with Tkinter
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
Financial profitability and solvency indicators
1161. Maximum Sum of Elements in Layer: Hierarchical Traversal Application Problems
MySQL---单行函数
Architecture Battalion Module 8 Homework
架构师04-应用服务间加密设计和实践
随机推荐
sqlite3 simple operation
给定一个ip地址,子网掩码怎么算网络号(如何获取ip地址和子网掩码)
MySQL---operator
学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
AI 自动写代码插件 Copilot(副驾驶员)
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
ResNet的基础:残差块的原理
Chinese encoding Settings and action methods return values
MySQL---Basic select statement
All-platform GPU general AI video supplementary frame super-score tutorial
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
MySQL - single function
spark报错OutOfMemory「建议收藏」
-xms -xmx(information value)
Mobile web development 02
【公开课预告】:超分辨率技术在视频画质增强领域的研究与应用
京东获取商品历史价格信息 API
UVM RAL模型和内置seq
c语言解析json字符串(json对象转化为字符串)
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao