当前位置:网站首页>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;
}
}
边栏推荐
- Django cannot link mysql database
- 【小程序】低代码+小游戏=小游戏可视化开发
- shell脚本------函数的格式,传参,变量,递归,数组
- 香港服务器如何进行加密?
- 李开复花上千万投的缝纫机器人,团队出自大疆
- Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
- 发明时代,「幂集创新」事关你我
- 西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录
- 报告:想学AI的学生数量已涨200%,老师都不够用了
- 微信一面:一致性哈希是什么,使用场景,解决了什么问题?
猜你喜欢
WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
Intranet penetration tool ngrok usage tutorial
报告:想学AI的学生数量已涨200%,老师都不够用了
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
MySQL 原理与优化,Group By 优化 技巧
Scala 高阶(七):集合内容汇总(上篇)
Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
ABP 6.0.0-rc.1的新特性
随机推荐
ABP 6.0.0-rc.1的新特性
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
#Internet of Things essay#Xiaoxiong pie equipment development actual combat
Manchester city launch emotional intelligence scarf can be detected, give the fans
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
LeetCode #101. Symmetric Binary Tree
基于STM32+铂电阻设计的测温仪
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
Intranet penetration tool ngrok usage tutorial
Ways to prevent data fraud
你没见过的《老友记》镜头,AI给补出来了|ECCV 2022
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
PM2 configuration file
李开复花上千万投的缝纫机器人,团队出自大疆
在北极都可以穿短袖了,温度飙升至32.5℃
MySQL principle and optimization of Group By optimization techniques
专业人士使用的 11 种渗透测试工具
发明时代,「幂集创新」事关你我
十分钟教会你如何使用VitePress搭建及部署个人博客站点