当前位置:网站首页>力扣 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;
}
边栏推荐
- Download exclusively | Alibaba cloud maxcompute questions and answers to unlock SaaS mode cloud data warehouse in this electronic manual!
- 嵌入式开发:技巧和窍门——设计强大的引导加载程序的7个技巧
- Seretod2022 track1 code analysis - task-based dialogue system challenge for semi supervised and reinforcement learning
- [RTOS training camp] task scheduling (Continued), task comity, scheduling summary, queue and evening class questions
- 【RTOS训练营】设备子系统、晚课学员提问
- Day06 MySql知识点总结
- 参数解析器HandlerMethodArgumentResolver分析与实战
- 2022/7/25 exam summary
- OAuth2和JWT
- AI knows everything: build and deploy sign language recognition system from 0
猜你喜欢

南姐的糗事

ASP.NET Core配置

Jupyter changes the main interface and imports the dataset

Hcip day 13

Using questpdf operation to generate PDF is faster and more efficient!

BGP 综合实验

超全的开源Winform UI库,满足你的一切桌面开发需求!
![[laser principle and application -3]: foreign brands of lasers](/img/8a/620544bb52c6b8c4db83f9a14783bb.png)
[laser principle and application -3]: foreign brands of lasers

【RTOS训练营】课程学习方法和C语言知识(指针、结构体、函数指针、链表)和学员问题

Talk about "people" in the R & D team
随机推荐
Redis (VIII) - redis enterprises' actual coupons spike
Set set learning
How to copy and paste QT? (QClipboard)
Lock upgrade: no lock, bias lock, lightweight lock, heavyweight lock
[RTOS training camp] GPIO knowledge and preview arrangement + evening class questions
Amin's confession
进程与线程
Biological JC uvssa complex alleviates myc driven transcription pressure ⼒ English
爬虫小操作
Compile openfoam solver with cmake
编程学习过程中有哪些快速提高编程技巧的方法?
MySQL pymysql operation
Openvino installation pit notes
【RTOS训练营】设备子系统、晚课学员提问
独家下载|《阿里云MaxCompute百问百答》 解锁SaaS模式云数据仓库尽在本电子手册!
【RTOS训练营】课程学习方法和C语言知识(指针、结构体、函数指针、链表)和学员问题
Unityvr robot Scene 3 gripper
Some abnormal error reports and precautions of flowable (1)
985高校副教授晒年薪,公积金顶普通人月薪,网友:不愧是在上海
Selenium assertion and JS actuator