【leetcode】Reverse Nodes in k-Group (hard)☆
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
思路:
我的想法是把每一组的翻转单独列出来,每次翻转时都顺次翻转跟着的元素,如果遇到数目不够的再把后面的翻转回去。
代码略长...
//start time = 10:36
//end time = 13:27
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
ListNode * anshead = NULL; //答案头结点
ListNode * anstail = NULL; //已经翻转过的链表的尾结点
ListNode * parthead = NULL; //当前组翻转后的头结点
ListNode * parttail = NULL; //当前组翻转后的尾结点
ListNode * nxthead = NULL; //下一次翻转时的头结点 int num = reverse(head, k, nxthead, parthead, parttail);
//链表总长度不足k 把部分翻转的再还原回去
if(num != )
{
head = parthead;
reverse(head, num, nxthead, parthead, parttail);
return parthead;
}
anshead = parthead; //确定答案头结点是第一次翻转后的头结点
//只要后面还有非空指针 就接着翻转下一组k个元素
while(nxthead != NULL)
{
anstail = parttail;
head = nxthead;
num = reverse(head, k, nxthead, parthead, parttail);
if(num != ) //该组不足k个元素 恢复后面的元素
{
head = parthead;
reverse(head, num, nxthead, parthead, parttail);
anstail->next = parthead;
return anshead;
} anstail->next = parthead; //把新翻转后的组加在答案的末尾
} return anshead;
} //翻转一组k个元素
//输入: head 当前需要翻转链表的头结点 k 需要翻转的个数
//输出: nxthead 下一次翻转时的头结点 parthead 当前组翻转后的头结点 parttail 当前组翻转后的尾结点
//返回 0表示完成翻转 返回非0表示不足k个
int reverse(ListNode *head, int k, ListNode * &nxthead, ListNode * &parthead, ListNode *&parttail)
{
//头为空 直接返回
if(head == NULL)
{
parthead = head;
return ;
} parttail = head; //当前k个元素组的链尾
parthead = head; //当前组的链头
ListNode * cur = head->next; //当前待翻转的元素
parttail->next = NULL; int n = k - ;
while(n--)
{
//不够k个 把已经翻转过的转回来
if(cur == NULL)
{
return k - n + ; //返回需要在尾部翻转回的链表长度
} ListNode * nxt = cur->next;
cur->next = parthead;
parthead = cur;
cur = nxt;
} nxthead = cur; return ;
}
};
看了别人精简的代码,每次先循环判断当前组是否够k个元素,省去了判断是否需要重新翻转的麻烦
O(n) and O(1) of course it is.
Each time when starting, find the future new head (nh) and save the old head of the next to-be-reversed list. If existed, nh is k step far from oh. Then reverse the list from oh to nh and save its tail for combination.
Example:
1-2-3-4-5
iter1: oh = 1, nh = 2 2-1-3-4-5
iter2: oh = 3, nh = 4 2-1-4-3-5
iter3: oh = 5, nh = 0 2-1-4-3-5
ListNode *reverseKGroup(ListNode *head, int k) {
if(!head || !head->next || k==) return head;
int i=k;
ListNode * p0 = head, * p1 = head->next, * p2 = , * t = , * ret = head, *oh, * nh;
while(p1)
{
oh = nh = p0;
i = k;
while(--i && nh)
nh = nh->next; //判断后面是否够k个元素
if(!nh) break; //不够直接跳出
i = k;
while(--i) //翻转后面的k个元素
{
p2 = p1->next;
p1->next = p0;
p0 = p1; //p0是一组的头结点
p1 = p2;
}
if(t) //已经翻转后的尾部加上新的元素
t->next = p0;
else
ret = p0; //确定头结点
p0 = oh;
t = p0; //最初当前组的头结点oh 是现在的尾部
p0 = p0->next = p1; //新的待判断的头结点
if(p1)
p1 = p1->next; //新的头结点的下一个结点
}
return ret;
}
【leetcode】Reverse Nodes in k-Group (hard)☆的更多相关文章
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】853. Car Fleet 解题报告(Python)
[LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- [译]git reset
git reset 如果说git revert是一个安全的撤销方式, 那么git reset就是一个非常危险的方法了. 当你使用git reset撤销的时候, 你没有可能在回到最初了-他是一个永久的不 ...
- ThinkPHP魔术方法
我们在使用thinkphp开发系统的时候,有时候会用到getById('1')这个方法快速的获取一条信息的内容,比用where(" id =1 ")->find();好用多了 ...
- 第2月第1天 命令(Command)模式
http://www.tracefact.net/Design-Pattern/Command.aspx 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请 ...
- 用phpcms开发模块时中文乱码问题
学着用phpcms开发一个文件管理模块,出现中文乱码,折腾了半天,总结如下 1.自己在mysql客户端建表,默认表格的编码为latin1_swedish_ci,虽然可以建好后用alter命令修改编码, ...
- windowSoftInputMode属性讲解
windowSoftInputMode属性讲解(下面这段内容我参考别人的博客,并加入我的一些意见) 我们从这个属性的名称中,可以很直观的看出它的作用,这个属性就是来设置窗口软键盘的交互模式的.andr ...
- Triangle
动态规划 int minimumTotal (vector<vector<int>>& triangle) { ; i >= ; --i) ; j < i ...
- 《lucene原理与代码分析》笔记
1.全文索引相对于顺序扫描的优势:一次索引,多次使用 2.创建索引的步骤:(1)要索引的原文档 (2)将原文档传给分词组件(Tokenizer)分词组件会做如下事情:(此过程称为Tokenize)a. ...
- Implement a TextView with an animation in its left side
In my case, I want to write a TextView with an animation in its left side. ImageView + TextView coul ...
- svn co 与ssl
默认情况下, yum安装的svn用的是GnuTLS, 而不是ssl, 导致checkout https协议打包的svn repo的时候会报错. 解决方法是用openssl重新编译安装svn.注意ssl ...
- JQuery知识点链接
1.深入理解jQuery插件开发 http://learn.jquery.com/plugins/basic-plugin-creation/ 2.jQuer ...