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 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
k个k个的节点反转,即可,用到的记录节点可能比较多,写的比较乱,代码如下所示:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(k == ) return head;
if(!head || !head->next)
return head;
int len = ;
ListNode * p1 = head;
while(p1){
p1 = p1->next;
len++;
}
//如果k比总长度还要长,那么直接返回即可
if(len < k) return head;
ListNode * prev = NULL;
ListNode * curr = head;
ListNode * tmpNode = NULL;
ListNode * ret = new ListNode(-);
ListNode * helper = curr;
ListNode * helperPre = NULL;
while(len >= k){
for(int i = ; i < k; i++){//区域反转
tmpNode = curr->next;
curr->next = prev;
prev = curr;
curr = tmpNode;
}
if(!ret->next){//第一次手下记下的是首节点的位置
ret->next = prev;
}else{
helperPre->next = prev;//以后将上一部分的尾节点连接到此部分的首节点上面
}
helper->next = curr;
prev = helper;
helper = curr;
helperPre = prev;
len -= k;
}
helperPre->next = curr;//将上次的尾节点域剩下的节点连接起来
return ret->next;
}
};
LeetCode OJ:Reverse Nodes in k-Group(K个K个的分割节点)的更多相关文章
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [LeetCode] 25. 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. k ...
- 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]25. 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. k ...
- [LeetCode]25. 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. k ...
- [LeetCode 题解]: Reverse Nodes in K-Groups
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode 【 Reverse Nodes in k-Group 】 python 实现
原题: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- [leetcode 25]Reverse Nodes in k-Group
1 题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- 【leetcode】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 and ret ...
- 【leetcode】Reverse Nodes in k-Group (hard)☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
随机推荐
- Use the SVN command-line tool
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...
- Appium移动自动化
一. 安装node.js 因为Appium是使用nodejs实现的,所以node是解释器,首先需要确认安装好 官网下载node.js:https://nodejs.org/en/download/ 安 ...
- 20145327 《Java程序设计》第八周学习总结
20145327 <Java程序设计>第八周学习总结 教材学习内容总结 NIO使用频道(channel)来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区(Buffer)容量,在缓冲区 ...
- 20145231熊梓宏 《网络对抗》 Bof逆向基础.shellcode注入
20145231网络对抗<逆向及Bof基础>shellcode注入 实验目的与要求 1.本次实践的对象是一个名为pwn1的linux可执行文件. 2.若该程序正常执行,则main函数会调用 ...
- ubuntu下 gedit中文乱码
Gedit 3.x 版本设置 (适用于Ubuntu 11.10及以后) 命令方式 gsettings set org.gnome.gedit.preferences.encodings auto-de ...
- iOS开发中的地图开发
显示地图: 1.导入头文件 #import <MapKit/MapKit.h> 如果同时需要用户定位的话还需要 #import <CoreLocation/CoreLocation. ...
- centos 查询mysql配置文件位置
具体指令: 1.which mysqld (”which 文件名“ : 搜索命令所在路径及别名) 2./usr/sbin/mysqld --verbose --help | grep -A 1 'D ...
- C#中将一个引用赋值null的作用
有类A,以及A类型的变量a和b.初始化a之后,将a赋给b.之后将a赋为null.之后b还是可以使用. 思维误区:本来以为a=null之后,b也应该等于null. 实际测试效果如下 class Prog ...
- shiro对事务的影响
记一个 No transaction aspect-managed TransactionStatus in scope 错误的解决方法 昨天出现一个BUG,事务没有加回滚成功,修改管理员密码事务没有 ...
- LeetCode——Find Duplicate Subtrees
Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...