当前位置:网站首页>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;
}
}
边栏推荐
- 2022 Niu Ke Duo School (6) M. Z-Game on grid
- 【微服务~远程调用】整合RestTemplate、WebClient、Feign
- "Digital Economy Panorama White Paper" Special Analysis of Banking Industry Intelligent Marketing Application Released
- 鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄...
- 【Untitled】
- 数据挖掘-05
- How should the acceptance criteria for R&D requirements be written?| Agile Practices
- K个结点的组内逆序调整
- MongoDB-查询中$all的用法介绍
- 又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
猜你喜欢
随机推荐
HAproxy: load balancing
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
标准C语言学习总结14
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
Scala 高阶(七):集合内容汇总(上篇)
阿里高工带来的20022最新面试总结太香了
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
Fragment中嵌套ViewPager数据空白页异常问题分析
HAproxy:负载均衡
Flutter入门进阶之旅(六)Layout Widget
Blazor Server (9) from scratch -- modify Layout
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
We really need DApp?Really can't meet our fantasy App?
Reading and writing after separation, performance were up 100%
听声辨物,这是AI视觉该干的???|ECCV 2022
自定义VIEW实现应用内消息提醒上下轮播
Scala Advanced (7): Collection Content Summary (Part 1)
The FFmpeg library is configured and used on win10 (libx264 is not configured)
#Internet of Things essay#Xiaoxiong pie equipment development actual combat