当前位置:网站首页>K个结点的组内逆序调整
K个结点的组内逆序调整
2022-08-09 12:01:00 【爱敲代码的Harrison】
题目
力扣链接:K个结点的组内逆序调整

代码
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;
}
}
边栏推荐
- How should the acceptance criteria for R&D requirements be written?| Agile Practices
- OpenSSF的开源软件风险评估工具:Scorecards
- 【Untitled】
- 基于STM32+铂电阻设计的测温仪
- FFmpeg在win10上编译安装(配置libx264)
- 在北极都可以穿短袖了,温度飙升至32.5℃
- 听声辨物,这是AI视觉该干的???|ECCV 2022
- 读写分离后,性能居然提升100%了呀
- Blazor Server (9) from scratch -- modify Layout
- Apexsqlrecover无法连接数据库
猜你喜欢

The latest interview summary in 20022 brought by Ali senior engineer is too fragrant

又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...

阿里高工带来的20022最新面试总结太香了

Scala Advanced (7): Collection Content Summary (Part 1)

水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...

How to upload local file trial version in binary mode in ABAP report

proto3-2语法

Shell正则表达式,三剑客之grep命令

【微服务~远程调用】整合RestTemplate、WebClient、Feign

中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
随机推荐
Web console control edit box
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
Blazor Server (9) from scratch -- modify Layout
goalng-sync/atomic原子操作
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
API调用,API传参,面向对接开发,你真的会写接口文档吗?
WPF implements a MessageBox message prompt box with a mask
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
荣耀携手Blue Yonder,加快企业战略增长
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
Senior told me that the giant MySQL is through SSH connection
专业人士使用的 11 种渗透测试工具
基于CAP组件实现补偿事务与幂等性保障
Batch大小不一定是2的n次幂!ML资深学者最新结论
900页数学论文证明旋转的黑洞不会爆炸,丘成桐:30多年来广义相对论首次重大突破...
Nature:猪死亡1小时后,器官再次运转
Blocking, non-blocking, multiplexing, synchronous, asynchronous, BIO, NIO, AIO all in one pot