No.025:Reverse Nodes in k-Group
问题:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
官方难度:
Hard
翻译:
给定一个链表,将节点每k项倒序链接,并且返回头结点。如果剩余链表不足k项,保持原状不动。
算法必须使用恒定的空间,且不能只交换节点的数据,必须交换节点。
例子:
给定链表:1->2->3->4->5。
K=2,返回链表:2->1->4->3->5。
K=3,返回链表:3->2->1->4->5。
- 这是No.024(Swap Nodes in Pairs)的深入研究。
- 需要交换具体的节点,而不是节点存储的数据。
- 首先要确定链表的长度size和返回的第一个节点first。
- 维护上一个节点last和当前节点current。
- 根据入参k和链表长度size建立while循环,每次循环结束前size-k。
- 循环内部,先将待处理的节点,放入数组。
- 将last节点的next指向数组最后一个元素,然后倒序将数组中的节点的next指向数组上一个元素,最后将数组第一个元素节点next指向当前节点current。
- 更新上一个节点值last。
- 入参检查,第一个节点head为null,或k<2时,直接返回head。
解题代码(交换数据):
// 交换数据
public static ListNode reverseKGroupVal(ListNode head, int k) {
if (head == null || k < 2) {
return head;
}
// 获取链表的长度
ListNode check = head;
int size = 0;
while (check != null) {
size++;
check = check.next;
}
ListNode current = head;
ListNode[] reverse = new ListNode[k];
while (size > k - 1) {
// 倒序的节点数组,同时将 current 指向下一次倒序的节点开始位置
for (int i = 0; i < k; i++) {
reverse[i] = current;
current = current.next;
}
// 交换至一半结束
for (int i = 0; i < (k >>> 1); i++) {
reverse[i].val = reverse[i].val + reverse[k - 1 - i].val - (reverse[k - 1 - i].val = reverse[i].val);
}
size -= k;
}
return head;
}
reverseKGroupVal
解题代码(交换节点):
// 交换节点
public static ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k < 2) {
return head;
}
// 获取链表的长度
ListNode check = head;
int size = 0;
while (check != null) {
size++;
check = check.next;
}
// 确定返回的头节点
ListNode first = head;
if (k <= size) {
for (int i = 1; i < k; i++) {
first = first.next;
}
}
// 当前节点和上一个节点
ListNode current = head;
ListNode last = new ListNode(0);
ListNode[] reverse = new ListNode[k];
while (size > k - 1) {
for (int i = 0; i < k; i++) {
reverse[i] = current;
current = current.next;
}
// 倒序赋值
last.next = reverse[k - 1];
for (int i = k - 1; i > 0; i--) {
reverse[i].next = reverse[i - 1];
}
reverse[0].next = current;
// 更新上一个节点
last = reverse[0];
size -= k;
}
return first;
}
reverseKGroup
相关链接:
https://leetcode.com/problems/reverse-nodes-in-k-group/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.025:Reverse Nodes in k-Group的更多相关文章
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- LeetCode之“链表”:Reverse Nodes in k-Group
题目链接 题目要求: Given a linked list, reverse the nodes of a linked list k at a time and return its modifi ...
- 每日算法之二十三:Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- LeetCode 025 Reverse Nodes in k-Group
题目描述:Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time an ...
- 【Reverse Nodes in k-Group】cpp
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
随机推荐
- 一则spring容器启动死锁问题(DefaultListableBeanFactory/DefaultSingletonBeanRegistry)
线上发现一个问题,应用在启动时会卡死,log上并没有什么异常输出,初判应该是死锁问题. 抓现场的thread dump文件, 确实是有两个线程有deadlock问题. 线程一 "HSFBiz ...
- Chrome开发者工具不完全指南(二、进阶篇)
上篇向大家介绍完了基础功能篇,这次分享的是Chrome开发工具中最有用的面板Sources. Sources面板几乎是我最常用到的Chrome功能面板,也是在我看来决解一般问题的主要功能面板.通常只 ...
- jQuery第一篇 (帅哥)
同学心目中的jQuery: 简单易用,功能强大,对移动端来说,体积稍大. 1.1 回顾前面学到的js我们遇到的一些痛点 window.onload 事件有个事件覆盖的问题,我们只能写一个 代码容错 ...
- python中configparser模块
python中的configparse模块的使用 主要用来解析一些常用的配置,比如数据配置等. 例如:有一个dbconfig.ini的文件 [section_db1] db = test_db1 ho ...
- Jquery中的(function($){...})(jQuery)
当你第一眼看到“(function($){...})(jQuery)”的时候,你有什么感觉?呵呵呵,我当时还是止不住的从心底里骂了一句——操,这他妈什么劳什子.时过境迁,对于现在无比倚重Jquery的 ...
- for循环或Repeat里面对某个字段进行复杂处理的解决方案
在后台用一个方法处理
- 【.NET深呼吸】存储基于本地线程的值
在特定情况,我们希望这样一个场景: N个线程同时调用同一个类实例的同一个操作方法,并且同一个变量可以面向每一个线程存储独立的值.比如,某变量X,它对于线程A的值与对于线程B的值是相互独立的.线程A设置 ...
- XenServer pool 移除server 设置master
如果因为Pool中Master主机由于某种原因导致失效,会引起整个Pool进入紧急模式,恢复步骤如下: 在成员服务器上输入如下命令 # xe host-emergency-ha-disable ...
- 《JS设计模式笔记》 2,简单工厂模式
<script type="text/javascript"> //简单工厂模式 //定义:由一个方法来决定到底要创建哪个类的实例,而这些实例经常拥有相同的接口.其实例 ...
- 为SubSonic3.0的查询(SubSonic.Query.Select和存储过程)添加更多的执行功能
在使用SubSonic3.0的查询功能时,会发现想通过执行返回我们想要的数据,切没有相关的功能,比如说:SubSonic.Query.Select,在使用查询时没有返回DataSet或DataTabl ...