当前位置:网站首页>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;
}
}
边栏推荐
- Web console control edit box
- win10编译x264库(也有生成好的lib文件)
- 程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
- 网页控制台控制编辑框
- "Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
- 1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
- 虚拟机安装出现的问题汇总
- 国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
- web course design
- 【小程序】低代码+小游戏=小游戏可视化开发
猜你喜欢
随机推荐
脱光衣服待着就能减肥,当真有这好事?
WeChat Mini Program Payment and Refund Overall Process
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
proto3-2语法
正则表达式(规则,匹配,和实际使用)
发明时代,「幂集创新」事关你我
electron 应用开发优秀实践
Scala 高阶(七):集合内容汇总(上篇)
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
Summary of learning stages (knapsack problem)
[Interview high-frequency questions] Linked list high-frequency questions that can be gradually optimized
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
Nature:猪死亡1小时后,器官再次运转
Batch大小不一定是2的n次幂!ML资深学者最新结论
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
Intranet penetration tool ngrok usage tutorial
十分钟教会你如何使用VitePress搭建及部署个人博客站点
标准C语言学习总结14