题目

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. Python高效开发实战——Django、Tornado、Flask、Twisted

    今天要推荐的就是这本书,内容涉及四种主流的Python Web开发框架,零基础完成网站搭建.数据库设计.前后端开发,全方位领悟Python原理与应用. 最新最全的框架实战,尽在这本书,可搜索亚马逊.京 ...

  2. Java static修饰符小记

    首先我们明确一点:java是一个纯面向对象的编程语言,它的每一个文件都视为一个类,当我们创建一个对象的时候,就是在调用一个文件,那么这个时候,这个文件(类)里的一些东西,它是需要通过对象来使用或访问, ...

  3. Webstrom使用手册小记

    全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/10883350.html,多谢~=.= 1.从git上拉取项目 或者 2.切换分支(webstro ...

  4. c++指针二维数组

    ; int** G; //初始化 G = new int*[N]; ; i < N; i++) G[i] = new int[N]: //删除 ; i < N; i++) delete[] ...

  5. JS.match方法 正则表达式

    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. <sc ...

  6. 比特币中P2PKH(pay-to-public-key-hash)的锁定脚本和解锁脚本

    脚本格式 P2PKH的锁定脚本为: OP_DUP OP_HASH160 PUSHDATA(<Cafe Public Key Hash>) OP_EQUALVERIFY OP_CHECKSI ...

  7. hdu-2255 奔小康赚大钱---KM模板

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2255 题目大意: Problem Description 传说在遥远的地方有一个非常富裕的村落,有一 ...

  8. [C++]#if !defined 的作用

    当你用VC的菜单新增一个类,你会发现自动生成的代码总是类似下面的样子: #if !defined(AFX_XXXX__INCLUDED_) #define  AFX_XXXX__INCLUDED_ 具 ...

  9. 架构图(拓扑图)画图工具分析整理(静态,动态,可交互图.层级tu)

    最近要画架构图. 一方面有图片洁癖,另外一方面又不想不停的挪动图片. 一开始想用脑图软件. 发现脑图是树状的,架构模块依赖图是网状的.(也可以简化为层级图,不画交互关系.类似 dubbo 的架构图. ...

  10. java面向对象思想1

    1.面向对象是面向过程而言.两者都是一种思想.面向过程:强调的是功能行为.(强调过程.动作)面向对象:将功能封装进对象,强调了具备了功能的对象.(强调对象.事物)面向对象是基于面向过程的.将复杂的事情 ...