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. 【转】通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法

    错误代码是1130,ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this MySQL server 是无法给远程连接的用 ...

  2. Win7窗口操作

    1.使用 Aero Shake 晃动最小化打开的窗口您可以使用 Aero Shake 晃动将所有打开的窗口快速最小化,只剩下您需要使用的窗口.然后,还可以轻松还原所有窗口.操作方法如下:1. 在需要保 ...

  3. JQ 动态添加节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 用自动化运维工具解放IT运维

    何谓自动化运维,即在最少的人工干预下,结合运用脚本与第三方工具,保证业务系统7*24小时高效稳定运行.这应该是所有业务系统运维终极目标. 我们对运维的要求通常是: 1.事前预警 在故障出现之前,管理人 ...

  5. 004 range的用法

  6. crontab指令详解

    引用:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/06/3002602.html 具体指令请参考文章:linux指令. 详细版推荐原 ...

  7. 从汇编看c++的虚拟继承以及其内存布局(一)

    先看第一种最简单的情形,所有类中没有任何虚函数的菱形继承. 下面是c++源码: class Top {//虚基类 public: int i; Top(int ii) { i = ii; } }; c ...

  8. 发现一个不错的学习git的地方

    Git入门:http://rogerdudler.github.io/git-guide/index.zh.html 简洁.实用.高效的学习git基本操作的方式

  9. Oracle表分区[转]

    废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库表中某一字段的值的范围来划分分区,例如: create table gra ...

  10. openstack安装记录(二)keystone安装

    先决条件 在你配置 OpenStack 身份认证服务前,你必须创建一个数据库和管理员令牌. 完成下面的步骤以创建数据库: 用数据库连接客户端以 root 用户连接到数据库服务器: $ mysql -u ...