【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 ...
随机推荐
- Python开发环境Wing IDE如何调试进程异常报告
Wing IDE的调试器所报告的任何异常,都会在调试器以外的任何代码运行事件中展示出来. 通过使用Debug工具或者是Debug菜单中的Start / Continue继续调试过程的异常检测. Win ...
- java.lang.ClassNotFoundException:org/apache/commons/collections/CursorableLinkedList
明明有 commons-collections.jar 将jar包复制到Tomcat的WEB-INF/lib下就可以了...
- 【ros depthimage_to_laser kinect2】
kinect2的深度图可以转换成激光来用,使用depthimage_to_laser 这个tf是用来给rviz显示的 1)开启kinect2 rosrun kinect2_bridge kinect2 ...
- jquery-weui picker组件实现只选择年月
var date = new Date() var month = date.getMonth()+1 //获取当前月份 $('#selectTime').picker({ toolbarTempla ...
- cms-帖子管理
mapper: <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC & ...
- COGS 201. [BYVoid S1] 埃雷萨拉斯的宝藏
★★ 输入文件:eldrethalas.in 输出文件:eldrethalas.out 简单对比时间限制:1 s 内存限制:256 MB 问题描述 一万两千年前,精灵还是在艾萨拉女王的 ...
- php使用GD库实现图片水印和缩略图——生成图片缩略图
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- ubuntu 16.4 安装配置IK6.3.2
1. 从官网下载对应的解析版本 https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v6.3.2 2. 配置环境 安装mvn ...
- 2018.5.14 XML文档类型定义----DTD
1.DTD概述 一个完全意义上的XML文件不仅仅是Well-fromed(格式良好的),而且还应该是使用了一些自定义的标记ValidatingXMl(有效的)文档也就是说他必须遵守文档类型的定义中已声 ...
- 题解 CF734A 【Anton and Danik】
本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...