Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard

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的更多相关文章

  1. [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 ...

  2. 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 算法思想:基本操作就是链表 ...

  3. 【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 ...

  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. If ...

  5. [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 ...

  6. 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 ...

  7. [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  ...

  8. [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  ...

  9. 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 ...

随机推荐

  1. android 数据存储之SharePreference 的几种方式

    1. 常见的 getSharedPreferences(String filename,mode) 指定sp文件的名称,生成的文件名为 filename.xml 2.getPreferences(mo ...

  2. ASP.net+SQL server2008简单的数据库增删改查 VS2012

    工具:VS2012 数据库:SQL server 简单说明:根据老师上课给的代码,进行了简单的改正适用于VS2012环境,包括注册.登录.查询.修改.删除功能,多数参考了网上的代码 百度云源代码连接t ...

  3. AutoFac初探

    .net 4.0使用的DLL #region RegisterType注册 var builder = new ContainerBuilder(); builder.RegisterType< ...

  4. rpm方式安装MySQL-5.6

    1. 卸载系统原有的mysql-libs rpm -e [name] --nodeps 2. 安装MySQL rpm -ivh MySQL-server-XXXXXX rpm -ivh MySQL-c ...

  5. 也谈谈关于WEB的感想

    距离上次在博客园发表博文已经是数年以前了,想想自己也确实有够懒惰的,实为不该. 引起我想发这篇博文的原因是 @Charlie.Zheng 所发表的 <Web系统开发构架再思考-前后端的完全分离& ...

  6. 使用runtime 实现weex 跳转原生页面

    一.简述 最近项目组打算引入weex,并选定了一个页面进行试水.页面很简单,主要是获取数据渲染页面,并可以跳转到指定的页面.跟之前使用RN 相比,weex 确实要简单很多.从下图中我们可以看到,wee ...

  7. Django学习(一) Django安装配置

    上一节介绍了如何搭建Python的开发环境,这次介绍一下如何搭建Django的开发环境. 第一.下载Django Django跟Python的版本对应 Django version Python ve ...

  8. 求绝对值,hdu-2003

    求绝对值 Problem Description 求实数的绝对值.   Input 输入数据有多组,每组占一行,每行包含一个实数.   Output 对于每组输入数据,输出它的绝对值,要求每组数据输出 ...

  9. python连接redis文档001

    Installation redis-py requires a running Redis server. See Redis’s quickstart for installation instr ...

  10. ASP.net(C#)批量上传图片(完整版)

    摘自:http://www.biye5u.com/article/netsite/ASPNET/2010/1996.html   这篇关于ASP.Net批量上传图片的文章写得非常好,偶尔在网上看到想转 ...