[Linked List]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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
ListNode* newHead=NULL,*p=head,*next=NULL;
while(p){
next = p->next;
p->next = newHead;
newHead = p;
p = next;
}
return newHead;
}
ListNode* reverseKGroup(ListNode* head, int k) {
if(k<=){
return head;
}
ListNode *pre=NULL,*next=NULL;
ListNode *cur=head;
ListNode *reverseHead = NULL;
int cnt = ;
while(cur){
if(cnt==){
reverseHead = cur;
}
next = cur->next;
++cnt;
if(cnt==k){
cur->next=NULL;
ListNode *newHead = reverseList(reverseHead);
pre ? pre->next = newHead : head=newHead;
reverseHead->next = next;
pre = reverseHead;
cur = next;
cnt = ;
}else{
cur = cur->next;
}
}
return head;
}
};
[Linked List]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 算法思想:基本操作就是链表 ...
- 【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 ...
- [LintCode] 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 ...
- 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 ...
- [Swift]LeetCode25. k个一组翻转链表 | 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. 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 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 ...
随机推荐
- 原生的UITableViewCell高度自适应,textLabel自动换行显示
/* * 设置子项cell **/ - (UITableViewCell *)getChildCell:(UITableView *)tableView and:(NSIndexPath *)inde ...
- (jQuery||Zepto).extend 的一个小问题
最近一直在搞移动端,也由于自己对jQuery比较熟悉,再加上Zepto提供了跟jQuery一样的API,所以就选择了Zepto作为开发框架. 由于是移动端开发,所以也应用了一些ES5新增的API,比如 ...
- jquery中attr()与prop()函数用法实例详解(附用法区别)
本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...
- cocos2d-x中的Tiled地图
cocos2d-x中的瓦片地图是通过tiledMap软件制作的,存档格式是.tmx格式.此软件的使用步骤简单总结如下: (1)制作瓦片地图 1 打开软件,软件界面如下图. 2. 新建地图(文件-> ...
- python cmd模块练习
# encoding=utf-8 import cmd import sys # cmd模块练习 class Client(cmd.Cmd): ''' 1)cmdloop():类似与Tkinter的m ...
- Lake Counting--poj2386
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23950 Accepted: 12099 D ...
- linux----用户与whoami
linux中的su 命令可以完成用户切换:如我们先由root切换到mysql用户可以这样做su - mysql whoami #这个时候linux会打印出mysql who am i #这个时候lin ...
- 再次优化NGINX+php-fpm上传
上次写了一篇nginx+php-fpm优化上传,一位博友留言介绍了,第三方nginx upload module http://www.grid.net.ru/nginx/upload.en.html ...
- oracle 创建表空间详细介绍
注意点: 1.如果在PL/SQL 等工具里打开的话,直接修改下面的代码中[斜体加粗部分]执行 2.确保路径存在,比如[D:\oracle\oradata\Oracle9i\]也就是你要保存文件的路径存 ...
- HeadFirst设计模式读书笔记(4)-工厂模式
工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类把实例化推迟到子类. 所有工厂模式都用来封装对象的创建.工厂方法模式通过让子类决定该创建的对象是什么,来达到将对象 ...