【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 ...
 
随机推荐
- iOS - 毛玻璃动画效果
			
声明全局变量 #define kMainBoundsHeight ([UIScreen mainScreen].bounds).size.height //屏幕的高度 #define kMainBou ...
 - https验证新发现-老知识
			
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 可以设置https全局的域名校验规则 HttpsURLConnecti ...
 - 撸了个 django 数据迁移工具 django-supertube
			
撸了个 django 数据迁移工具 django-supertube 支持字段映射和动态字段转化. 欢迎 star,issue https://github.com/FingerLiu/django- ...
 - cd ..和cd -
			
cd ..是返回上一层目录, cd -是返回到上一次的工作目录.
 - 漫谈 Clustering (5): Hierarchical Clustering
			
系列不小心又拖了好久,其实正儿八经的 blog 也好久没有写了,因为比较忙嘛,不过觉得 Hierarchical Clustering 这个话题我能说的东西应该不多,所以还是先写了吧(我准备这次一个公 ...
 - subline 安装  package control
			
subline text2 输入 import urllib2,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa154 ...
 - PHP开发框架流行度排名:Laravel居首
			
摘要:在PHP开发中,选择合适的框架有助于加快软件开发,节约宝贵的项目时间,让开发者专注于功能的实现上.Sitepoint网站做了一个小的调查,结果显示最流行的PHP框架前三甲为:Laravel.Ph ...
 - java编程基础——栈压入和弹出序列
			
题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...
 - js字符串的使用
			
Javascript的内置功能之一就是字符串连接,如果+号用于两个字符串连接 var s="hello,world" //想要查找给定位置的字符 s.cha ...
 - MySQL - RIGHT JOIN
			
RIGHT JOIN 关键字 RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行. RIGHT JOIN 关键字语 ...