当前位置:网站首页>Intra-group reverse order adjustment of K nodes
Intra-group reverse order adjustment of K nodes
2022-08-09 13:16:00 【Harrison who loves to code】
题目
力扣链接:KIntra-group reverse order adjustment of nodes
代码
package com.harrison.class06;
/** * @author Harrison * @create 2022-06-19-15:50 * @motto 众里寻他千百度,蓦然回首,那人却在灯火阑珊处. */
public class Code07_ReverseNodesInKGroup {
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public ListNode reverseKGroup(ListNode head, int k) {
ListNode start=head;
ListNode end=getKGroupEnd(start,k);
if(end==null){
return head;
}
// 第一组凑齐了
head=end;
reverse(start,end);
ListNode lastEnd=start;
while(lastEnd.next!=null){
start=lastEnd.next;
end=getKGroupEnd(start,k);
if(end==null){
return head;
}
reverse(start,end);
lastEnd.next=end;
lastEnd=start;
}
return head;
}
public static ListNode getKGroupEnd(ListNode start,int k){
while(--k!=0 && start!=null){
start=start.next;
}
return start;
}
public static void reverse(ListNode start,ListNode end){
end=end.next;
ListNode pre=null;
ListNode next=null;
ListNode cur=start;
while(cur!=end){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
start.next=end;
}
}
边栏推荐
- Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
- FFmpeg compiles and installs on win10 (configure libx264)
- 放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
- The core key points of microservice architecture
- 非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
- 世界第4疯狂的科学家,在103岁生日那天去世了
- 你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
- MySQL5.6到8.0的账号迁移
- 用皮肤“听”音乐,网友戴上这款装备听音乐会:仿佛住在钢琴里
- LeetCode #101. 对称二叉树
猜你喜欢
Reading and writing after separation, performance were up 100%
虚拟机安装出现的问题汇总
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
【Untitled】
【HCIP持续更新】IS-IS协议原理与配置
Flutter入门进阶之旅(六)Layout Widget
C# Get system installed .NET version
win10编译x264库(也有生成好的lib文件)
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
AQS Synchronization Component - FutureTask Analysis and Use Cases
随机推荐
微信一面:一致性哈希是什么,使用场景,解决了什么问题?
Nature:猪死亡1小时后,器官再次运转
Flutter入门进阶之旅(一)-初识Flutter
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
系统提供的堆 VS 手动改写堆
Golang学习之路(五):Golang的函数
Win10 compiles the x264 library (there are also generated lib files)
Flutter入门进阶之旅(七)GestureDetector
Adalvo acquires its first branded product, Onsolis
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
【小程序】低代码+小游戏=小游戏可视化开发
GRPC整体学习
Manchester city launch emotional intelligence scarf can be detected, give the fans
Shell之常用小工具(sort、uniq、tr、cut)
How to upload local file trial version in binary mode in ABAP report
shell脚本------函数的格式,传参,变量,递归,数组
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
How should the acceptance criteria for R&D requirements be written?| Agile Practices
FFmpeg compiles and installs on win10 (configure libx264)
微服务架构的核心关键点