【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.
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的更多相关文章
- [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 算法思想:基本操作就是链表 ...
- 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 ...
- 【Binary Tree Post order Traversal】cpp
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 【Maximum Depth of Binary Tree 】cpp
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 【Unique Binary Search Trees II】cpp
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 【Binary Tree Level Order Traversal】cpp
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【Binary Tree Right Side View 】cpp
题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...
随机推荐
- c语言数据结构:用标志位实现循环队列
#include<stdio.h> #include<stdlib.h> #define MAXSIZE 10//定义队列长度 ;//定义标志位 typedef struct ...
- aar、jar、so的引入和aar打包包含so、aar、jar文件
so依赖 1,先建本地仓库,指向so放置的目录
- Oracle数据库基础--建表语法+操作
语法 1.建表 create table 表名( 列名 数据类型, …… ); 2.删除表:drop table 表名; 3.添加列:alter table 表名 add(列名 数据类型); 4.修改 ...
- OpenSSL context 的几个参数
NAME SYNOPSIS DESCRIPTION NOTES BUGS RETURN VALUES EXAMPLES SEE ALSO NAME SSL_CTX_set_verify, SSL_se ...
- 国密SM4分组加密算法实现 (C++)
原博客 :http://blog.csdn.net/archimekai/article/details/53095993 密码学的一次课程设计,学习了SM4加密算法,目前应用于无线网安全. SM4分 ...
- Windows环境下的Chocolatey安装使用
Chocolatey是一个软件包管理工具,类似于Ubuntu下面的apt-get,不过是运行在Windows环境下面 电脑 Powershell 方法/步骤 安装 Chocolatey的安装需要: P ...
- oo作业第四单元总结暨结课总结
目录 一.第四单元作业架构设计 1.第一次UML作业架构设计 2.第二次UML作业架构设计 二.架构设计和OO方法理解演进 三.测试理解与实践的演进 四.课程收获总结 五.三个具体改进建议 一.第四单 ...
- 【luogu P1637 三元上升子序列】 题解
题目链接:https://www.luogu.org/problemnew/show/P1637 BIT + 离散化. 读题得数据规模需离散化.BIT开不到longint这么大的数组. 对于题目所求的 ...
- 箭头函数 -------JavaScript
本文摘要:http://www.liaoxuefeng.com/ ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一 ...
- 关于Star UML
为什么是使用Star UML而不是Visio 2013呢? 以前本人在大学期间使用的Visio 2013来绘制UML的,最近一个星期因为在阅读源码,所以有多学了一门UML绘制工具—Star UML,下 ...