当前位置:网站首页>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;
}
}
边栏推荐
- MongoDB-查询中$all的用法介绍
- Scala Advanced (7): Collection Content Summary (Part 1)
- 【无标题】
- 字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
- Batch大小不一定是2的n次幂!ML资深学者最新结论
- Experiment record: the process of building a network
- JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
- 非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
- Summary of learning stages (knapsack problem)
- PM2 configuration file
猜你喜欢
随机推荐
WeChat Mini Program Payment and Refund Overall Process
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
Summary of learning stages (knapsack problem)
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
WPF implements a MessageBox message prompt box with a mask
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
内网穿透工具ngrok使用教程
LeetCode #101. Symmetric Binary Tree
世界第4疯狂的科学家,在103岁生日那天去世了
微信支付开发流程
香港服务器如何进行加密?
【Untitled】
ThreadLocal的简单理解
网页控制台控制编辑框
MySQL principle and optimization of Group By optimization techniques
electron 应用开发优秀实践
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
Reading and writing after separation, performance were up 100%
Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
[Interview high-frequency questions] Linked list high-frequency questions that can be gradually optimized