当前位置:网站首页>LeetCode 25. K 个一组翻转链表
LeetCode 25. K 个一组翻转链表
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/reverse-nodes-in-k-group/submissions/
给一个链表,一个k值,要求每k个节点为一组,进行翻转,不足k的不用管;
// 每k个节点是一组,对这组的节点进行翻转。
// 如果不够k个,就不用翻转了。
public ListNode reverseKGroup(ListNode head, int k) {
// 需要设置一个虚拟节点
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode node = dummyHead;
ListNode frist = head;
ListNode tail = head;
ListNode cur = head;
while(cur != null) {
for(int i=0;i<k-1;i++) {
tail = tail.next;
if(tail == null) {
return dummyHead.next;
}
}
// 截取k个节点,断开
ListNode next = tail.next;
tail.next = null;
node.next = null;
// 翻转
ListNode result = reverseList(frist);
frist.next= next;
node.next = tail;
node = frist;
cur = next;
frist = cur;
tail = cur;
}
return dummyHead.next;
}
private ListNode reverseList(ListNode head) {
ListNode first= null;
ListNode cur = head;
while(cur != null) {
ListNode next = cur.next;
cur.next = first;
first = cur;
cur = next;
}
return first;
}
边栏推荐
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 3款不同类型的自媒体免费工具,有效提高创作、运营效率
- L2 applications from a product perspective: why is it a playground?
- 不止跑路,拯救误操作rm -rf /*的小伙儿
- 力扣练习——62 有效的数独
- 老板加薪!看我做的WPF Loading!!!
- Double.doubleToLongBits() method uses
- 【机器学习】浅谈正规方程法&梯度下降
- Emulate stm32 directly with proteus - the programmer can be completely discarded
- 【勇敢饭饭,不怕刷题之链表】有序链表的合并
猜你喜欢

Some tips for using Unsafe

Article take you understand interrupt the key driver of polling mechanism

模块九 - 设计电商秒杀系统

Since the media hot style title how to write?Taught you how to write the title

网络基础(第一节)

接口定义与实现

从脚本到剪辑,影像大师亲授的后期制作秘籍

再有人问你分布式事务,把这篇扔给他

mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded

Flutter气泡框实现
随机推荐
Spss-多元回归案例实操
L2 applications from a product perspective: why is it a playground?
LAXCUS分布式操作系统安全管理
快速上手,征服三种不同分布式架构调用方案
从产品维度来看 我们为什么不能完全信任Layer2?
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
力扣练习——64 最长和谐子序列
Nocalhost - 让云原生时代的开发更高效
力扣练习——59 从二叉搜索树到更大和树
态路小课堂丨如何为CXP光模块选择光纤跳线?
A case of violent parameter tuning in machine learning
皕杰报表在传参乱码
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
Buckle Exercise - 61 Sort by frequency of characters
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
HDU 4372:Count the Buildings (Stirling数)
HDU 4135:Co-prime (容斥原理)
Where can I view the version record of WeChat applet submission review history?
实现内网穿透的最佳解决方案(无实名认证,完全免费)
力扣练习——61 根据字符出现频率排序