题目

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* reverseKGroup(ListNode* head, int k) {
if ( !head || !(head->next) || k< ) return head;
ListNode dummy(-);
dummy.next = head;
ListNode *beginK = &dummy;
ListNode *endK = &dummy;
while ( true )
{
// move forward k steps
for ( size_t i = ; i < k && endK; ++i ) {endK = endK->next;}
// check if already move to the end of linked list
if (!endK) break;
// reverse from beginK to endK
ListNode *curr = beginK->next;
for ( size_t i = ; i < k-; ++i )
{
ListNode *tmp = curr->next;
curr->next = tmp->next;
tmp->next = beginK->next;
beginK->next = tmp;
}
beginK = curr;
endK = curr;
}
return dummy.next;
}
};

Tips

这道题是Reverse Linked List(http://www.cnblogs.com/xbf9xbf/p/4464896.html)的加强版。

大概分两步:

1. 判断这一group是否够k个元素

2. 将这一group的k个元素reverse

每次reverse之前,都要构造成如下的条件。

结合代码以及下面的示例:以k=3为例

beginK→1(curr)→2→3(endK)→...

beginK:这一group之前的那个ListNode

curr: 这一group的第一个元素

endK:这一group的最后一个元素

3. 注意每次reverse完成后,迭代beginK和endK的值。

4. 注意循环终止的条件是endK移动到的NULL。

代码的风格追求简洁,因为简洁往往方便理解。

=============================================

第二次过这道题,一次AC了。这道题主要考查reverse linked list的操作。

/**
* 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 ( !head || k< ) return head;
ListNode dummpy(-);
dummpy.next = head;
ListNode* p1 = &dummpy;
ListNode* p2 = &dummpy;
for ( int i=; i<k && p2; ++i ) p2 = p2->next;
while ( p2 )
{
// reverse Nodes in one group
ListNode* curr = p1->next;
for ( int i=; i<k-; ++i )
{
ListNode* tmp = curr->next;
curr->next = tmp->next;
tmp->next = p1->next;
p1->next = tmp;
}
p1 = curr;
p2 = curr;
// if existing next k-group Nodes
for ( int i=; i<k && p2; ++i ) p2 = p2->next;
}
return dummpy.next;
}
};

【Reverse Nodes in k-Group】cpp的更多相关文章

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

  4. 【Binary Tree Post order Traversal】cpp

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  5. 【Maximum Depth of Binary Tree 】cpp

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  6. 【Minimum Depth of Binary Tree】cpp

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  7. 【Unique Binary Search Trees II】cpp

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  8. 【Binary Tree Level Order Traversal】cpp

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  9. 【Binary Tree Right Side View 】cpp

    题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...

随机推荐

  1. 查看mysql表和数据库的大小

    转自:http://xiaosu.blog.51cto.com/2914416/687835 1.查看数据库的大小 use 数据库名SELECT sum(DATA_LENGTH)+sum(INDEX_ ...

  2. MAC MAMP install yaf

    Yaf doesn't support PHP 5.6! You may see this: Warning: PHP Startup: yaf: Unable to initialize modul ...

  3. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getSer

    这其实就是 获得应用的根url,比如说你的应用的根路径是 http://localhost:8080,那么你列出的代码就是为basePath赋值为 http://localhost:8080.具体点: ...

  4. selenium跳过https的问题

    背景: 周六产品给我反馈:支付成功页面后会提示这个,问自动化为什么没有发现这样的问题 第一反应:这个地址肯定被举报了,我也肯定没有设置过安全链接,因为都没有见过这样的网址,如果有问题,应该会直接出错, ...

  5. 【51nod1743】雪之国度(最小生成树+倍增)

    点此看题面 大致题意: 给你一张无向连通图,其中每条边的边权为这条边连接的两点的权值之差.每次询问两点之间是否存在两条不重复的路径,若存在则输出这两条路径上最大值的最小值. 大致思路 这题显然就是要让 ...

  6. 【BZOJ1013】[JSOI2008] 球形空间产生器(高斯消元)

    点此看题面 大致题意: 给定一个\(n\)维球体上的\(n+1\)个点,请你求出这个球体的圆心的位置. 列出方程 这一看就是一道解方程题. 我们可以设这个球体的圆心的位置为\((x_1,x_2,..x ...

  7. 【转】iOS 上常用的两个功能:点击屏幕和return退出隐藏键盘和解决虚拟键盘挡住UITextField的方法

    iOS上面对键盘的处理很不人性化,所以这些功能都需要自己来实现, 首先是点击return和屏幕隐藏键盘 这个首先引用双子座的博客 http://my.oschina.net/plumsoft/blog ...

  8. js中随机数获取

    // 结果为0-1间的一个随机数(包括0,不包括1) var randomNum1 = Math.random(); //console.log(randomNum1); // 函数结果为入参的整数部 ...

  9. IDEA搭建Maven 的聚合项目

    今天突然想把自己学习在eclipse上的maven聚合项目搭建到IDEA上,结果IDEA有太多的配置步骤,导致失败了很多次,终于在网上找到了一篇博客 https://blog.csdn.net/for ...

  10. linux apache 不解析php文件显示源码

    首先检查是否安装PHP 没有的话就先安装 如果安装过 在/etc/httpd/conf/httpd.conf文件中 在<IfModule mime_module>里面 AddType ap ...