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 ...
随机推荐
- Node.js开发者最常范的10个错误
目录 前言 1 不使用开发工具 1.1 自动重启工具 1.2 浏览器自动刷新工具 2 阻塞event loop 3 频繁调用回调函数 4 圣诞树结构的回调(回调的地狱) 5 创建一个大而完整的应用程序 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (11) -----第三章 查询之异步查询
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第三章 查询 前一章,我们展示了常见数据库场景的建模方式,本章将向你展示如何查询实体 ...
- CentOS On VirtualBox
背景 后台开发需要随时与服务器交互,本人使用Mac开发.但是不愿意在Mac上直接安装redis以及mysql等等工具.所以选择在VirtualenvBox下安装一个服务器系统,并且使用ssh与其连接. ...
- Oracle 超长字符串分割劈分
Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...
- Nginx内置变量
$args #请求中的参数值 $query_string #同 $args $arg_NAME #GET请求中NAME的值 $is_args #如果请求中有参数,值为"?",否则为 ...
- mac下tomcat的安装与配置
1.到 apache官方主页 下载 Mac 版本的完整 tar.gz文件包.解压拷贝到 /Library目录下,并命名为Tomcat,其他目录也可. 2.修改目录权限 到终端输入 sudo chm ...
- C#实现二维码功能,winform 以及 asp.net均可以用
二维码现在用途很多,怎么用C#编程生成呢? 准备 ThoughtWorks.QRCode.dll 需要 一个类 QRCode,这个类专门来生成二维码的 using System; using Syst ...
- Android NDK开发Hello Word!
在之前的博客中已经为大家介绍了,如何在win环境下配置DNK程序,本篇我将带大家实现一个简单的Hello jni程序,让大家真正感受一下NDK开发的魅力.这里我们选择使用C+JAVA开发Android ...
- 在Linux(ubuntu server)上面安装NodeJS的正确姿势
上一篇文章,我介绍了 在Windows中安装NodeJS的正确姿势,这一篇,我们继续来看一下在Linux上面安装和配置NodeJS. 为了保持一致,这里也列举三个方法 第一个方法:通过官网下载安装 h ...
- geotrellis使用(十六)使用缓冲区分析的方式解决投影变换中边缘数据值计算的问题
Geotrellis系列文章链接地址http://www.cnblogs.com/shoufengwei/p/5619419.html 目录 前言 问题探索 采样说明 实现方案 总结 一.前言 ...