当前位置:网站首页>力扣 25. K 个一组翻转链表
力扣 25. K 个一组翻转链表
2022-07-26 00:50:00 【三更鬼】
题目来源:https://leetcode.cn/problems/reverse-nodes-in-k-group/
大致题意:
给一个链表和整数 k,将链表从头开始每 k 个翻转一次,若最后一段个数不足 k 个,则无需翻转
思路
- 设计一个翻转链表的函数,给定节点 node 和长度 k,可以将 node 开始的 k 个节点翻转。设计时需要注意,翻转后 node.next 需要指向翻转前该段链表最后一个节点的 next
- 使用一个变量存下当前待翻转的链表段的开始节点,遍历原链表,每遍历 k 个时,进行翻转,翻转后需要注意,当前链表段首节点的前个节点的 next 需要指向翻转后的首节点
具体看代码
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 1) {
return head;
}
// 当前遍历的节点数目
int count = 0;
// 当前翻转的链表段的开始节点
ListNode newHead = head;
// 当前遍历的节点
ListNode cur = head;
// 哨兵
ListNode ans = new ListNode(-1);
// 上一段翻转链表的尾节点
ListNode lastTail = ans;
while (cur != null) {
// 更新遍历的节点数目
count++;
// 若遍历节点的数目等于 k,进行翻转
if (count == k) {
// 首先存下当前段原头节点,也就是反转后的尾节点
ListNode newTail = newHead;
// 反转链表,同时存下段翻转后的头节点
newHead = reverse(newHead, k);
// 上一段的尾节点的 next 要指向反转后的头节点
lastTail.next = newHead;
// 重置计数
count = 0;
// 更新上一段的尾节点
lastTail = newTail;
// 更新下一段翻转前的首节点
newHead = newTail.next;
// 更新当前遍历的位置
cur = lastTail;
}
cur = cur.next;
}
return ans.next;
}
/** * 将从给定 head 节点开始的 k 个节点翻转 * @param head * @param k * @return */
public ListNode reverse(ListNode head, int k) {
// 当前翻转的长度
int count = 1;
// 上一个便利的节点
ListNode last = head;
// 当前遍历的
ListNode cur = head.next;
// 临时变量
ListNode temp;
// 若已翻转的长度不等于 k 时,继续翻转
while (count != k) {
// 当前翻转的长度 +1
count++;
// 存下要遍历的下个节点
temp = cur.next;
// 翻转
// 将当前链表指针指向上一个节点
cur.next = last;
// 更新上一个遍历的节点
last = cur;
// 更新下一个遍历的节点
cur = temp;
}
// newTail -> oldTail.next
// head 即为翻转后的第 k 个节点,它的 next 要指向原来第 k 个节点的 next,也就是第 k + 1 个节点
head.next = cur;
// 返回翻转后的首节点
return last;
}
边栏推荐
- Tensorflow 2 detailed explanation (TF ecosystem, installation, housekeeping, basic operation)
- Amin's confession
- Spine_附件皮肤
- ZABBIX monitoring host and resource alarm
- 如何才能修炼成一名不可替代的程序员?
- El table scroll bar settings
- JDBC implements the addition, deletion, modification and query of MySQL 8.0 database
- LCA three postures (multiplication, tarjan+ joint search set, tree chain dissection)
- 以数据驱动管理转型,元年科技正当时
- Redis Command Reference Manual - key
猜你喜欢

Tarjan finds the strongly connected component o (n+m), shrinking point
![[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions](/img/cb/de38e9f4186c4392ef8832659422ff.jpg)
[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions

Getting started with D3D calculation shaders

How can a team making facial mask achieve a revenue of more than 1 million a day?
![[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training](/img/56/7a49f9c825d88a31b980a5fae50951.png)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training

用 QuestPDF操作生成PDF更快更高效!

LCA three postures (multiplication, tarjan+ joint search set, tree chain dissection)

With data-driven management transformation, the first year of science and technology was at the right time

Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock

旅行+战略加速落地 捷途新产品矩阵曝光
随机推荐
If the native family is general, and the school is also a college on the rotten street, how to go on the next journey
【RTOS训练营】设备子系统、晚课学员提问
Hcip day 11
Azure synapse analytics Performance Optimization Guide (1) -- optimize performance using ordered aggregate column storage indexes
Force deduction record: Sword finger offer (2) -- jz13-22
C # from entry to mastery (III)
LVGL官方+100ASK合力打造的中文输入(拼音输入法)组件,让LVGL支持中文输入!
Biological JC uvssa complex alleviates myc driven transcription pressure ⼒ English
JDBC实现MySQL8.0数据库的增删改查
[oops framework] interface management
[array related methods in numpy]
MySQL pymysql operation
我们没有退路了
Leetcode notes 20. valid parentheses
南姐的糗事
el-table滚动条设置
测试左移和测试右移的概念
如何才能修炼成一名不可替代的程序员?
MMOCR使用指南
【RTOS训练营】GPIO知识和预习安排 + 晚课提问