当前位置:网站首页>K个一组翻转链表[链表拆解/翻转/拼装]
K个一组翻转链表[链表拆解/翻转/拼装]
2022-06-22 18:01:00 【REN_林森】
K个一组翻转链表
前言
对于链表操作而言,翻转链表是基础操作,而保证不断链是关键问题。不断链的本质就是把后面的被动节点先拼装上/用指针指着。
一、K个一组翻转链表

二、链表拆解/翻转/拼装
package everyday.hard;
public class ReverseKGroup {
/* target:把连续k个节点进行翻转。 选好k个节点,然后翻转,返回新的头节点。然后拼接上去。 */
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dummyHead = new ListNode(0, head);
ListNode begin = dummyHead, p = dummyHead;
int cnt = 0;
while (p.next != null) {
cnt = 0;
while (cnt < k && p.next != null) {
p = p.next;
cnt++;
}
// 有五个节点,可以翻转。
if (cnt % k == 0) {
// 拿到翻转之后的尾节点。
ListNode newTail = begin.next;
// 记录剩下的链表部分,防止丢失。
ListNode next = p.next;
// 再把链表切断,并进行翻转。
p.next = null;
begin.next = reverseList(newTail);
//把后面的链表接上。
newTail.next = next;
// 更新新一轮的开始节点。
begin = p = newTail;
}
}
return dummyHead.next;
}
// 翻转链表并返回新的头节点。
private ListNode reverseList(ListNode l) {
ListNode dummyHead = new ListNode();
while (l != null) {
// 头插法
ListNode next = l.next;
l.next = dummyHead.next;
dummyHead.next = l;
// 走向下一个需要插入的节点。
l = next;
}
return dummyHead.next;
}
// Definition for singly-linked list.
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;
}
}
}
总结
1)链表的翻转(头插法),多次刷题之后突然冒出的新写法。除此之外,以前喜欢用for循环/递归来改变链表方向也是可以的,只是注意一下返回新的头节点。
2)单向链表断链问题。
参考文献
边栏推荐
- 组合学笔记(五)分配格中的链
- Exness sorted out three problems to be solved in Musk's acquisition of Twitter
- 贪心之区间问题(1)
- jniLibs.srcDirs = [‘libs‘]有什么用?
- 实现领域驱动设计 - 使用ABP框架 - 解决方案概览
- Notes on new reports
- C#,入门教程——关于函数参数ref的一点知识与源程序
- Implementing Domain Driven Design - using ABP framework - solution overview
- shell脚本(五)——函数
- China's games are "harvesting" foreigners
猜你喜欢

新人报道的笔记
![Programmer's tool encyclopedia [continuous update]](/img/7e/b7fbc030f4bbded3ee6188360d7d54.png)
Programmer's tool encyclopedia [continuous update]
![[suggestions collection] common usage scenarios of message queue](/img/58/87dae469e5142507f2a5d100975b8e.png)
[suggestions collection] common usage scenarios of message queue

Activity跳转到Fragment的方法(Intent)

Active directory user logon Report

JVM quick start

机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效

RobotFramework 安装教程

C sqlsugar, hisql, FreeSQL ORM framework omni-directional performance test comparison sqlserver

Detailed explanation of shell script (x) -- how to use sed editor
随机推荐
5g short message solution
Is flush easy to open an account? Is it safe to open a mobile account?
Paopao Mart: empty souls need stories
Sre is bound to move towards the era of chaotic engineering -- Huawei cloud chaotic engineering practice
China's games are "harvesting" foreigners
贪心之区间问题(2)
std::enable_ shared_ from_ This error: error: expected template name before '<' token
Vs Code suddenly fails to jump
Flutter系列-Dart基础语法学习
How much do you know about the bloom filter and cuckoo filter in redis?
Several important viewpoints on operation and maintenance, monitoring and aiops
Zynq UltraScale + RFSoC ZCU111 RF时钟树学习 1
如何提高工作效率?苹果电脑效率工具合集
Service实战:使用Service完成一个下载任务
同花顺难开户么?网上开户安全么?
有效的括号
下拉刷新及上拉加载更多的ListView
Programmer's tool encyclopedia [continuous update]
函数的导数与微分的关系
3GPP 5G R17标准冻结,RedCap作为重要特性值得关注!