【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 ...
随机推荐
- javascript客户端检测技术
1. Firefox Gecko是firefox的呈现引擎.当初的Gecko是作为通用Mozilla浏览器一部分开发的,而第一个采用Gecko引擎的浏览器是Netscape6: 我们可以使用用户代理 ...
- Ubuntu 12 修改环境变量
Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环境变量.系统环境变量对所有系统用户都有效,用户环境变量仅仅对当前的用户有效. 修改用户环境变量 用户环境变量通常被存储在下面的文件中: ...
- php数据结构与算法
php面试题之二--数据结构和算法(高级部分) 二.数据结构和算法 1.使对象可以像数组一样进行foreach循环,要求属性必须是私有.(Iterator模式的PHP5实现,写一类实现Iterator ...
- HDU 1532 最大流模板题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1532 最近在学网络流,学的还不好,先不写理解了,先放模板... 我觉得写得不错的博客:http://blo ...
- Android学习笔记(二十)——自定义内容提供器
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 如果我们想要实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 Conten ...
- 使用srvany.exe将任何程序作为Windows服务运行
使用srvany.exe将任何程序作为Windows服务运行 2011 年 3 月 7 日 !本文可能 超过1年没有更新,今后内容也许不会被维护或者支持,部分内容可能具有时效性,涉及技术细节或者软件使 ...
- CCF 模拟D 动态规划
http://115.28.138.223:81/view.page?opid=4 这道题写的我醉醉的,想建一棵指定深度的树最后统计满足条件的个数 居然没去考虑这样必然超时!!!代码写的也是醉了,把没 ...
- IOI2015 Boxes
Description 给出一个环形,n个点,每次只能访问k个点,求最短距离. Sol 贪心. CCF的题解. 首先只会最多走一趟环形,根据抽屉原理,如果一边不足k个才会到另一边,所以对于第二次以上的 ...
- 分治法避免定义多个递归函数,应该使用ResultType
总结:对二叉树应用分治法时,应避免定义多个递归函数,当出现需要递归求解多种的结果时,尽量使用ResultType来让一次递归返回多种结果. 题目:Binary Tree Maximum Path Su ...
- 解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now
先查看文件真正的属性是什么? [root@xxxxx ~]# tar -zxvf tcl8.4.16-src.tar.gz gzip: stdin: not in gzip format tar: ...