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)☆的更多相关文章

  1. 【LeetCode】143. Reorder List 解题报告(Python)

    [LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  3. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  4. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  5. 【LeetCode】853. Car Fleet 解题报告(Python)

    [LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  6. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  7. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  8. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  9. 【LeetCode】809. Expressive Words 解题报告(Python)

    [LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  10. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

随机推荐

  1. css3动画由浅入深总结

    阅读目录 一:过渡动画---Transitions 二:Animations功能 三:translate(tx,ty) 四:scale(x,y) 五:rotate(x): 5.1:skew(x,y): ...

  2. mac jdk环境变量

    /System/Library/Java/JavaVirtualMachines/1.6.0.jdk /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk ...

  3. Visual Studio 各种版本的快捷键总结

    下列快捷组合键可在工具和文档窗口中用于进行移动.关闭或导航. 命令名 快捷键 说明 视图.全屏 SHIFT + ALT + ENTER 在打开和关闭之间切换“全屏”模式. 视图.向后定位 CTRL + ...

  4. 第11天 Stack Queue

    1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...

  5. Linux 动态监听进程shell

    背景 前几天在研究线程的时候,看到一句话说java里的线程Thread.run都会在Linux中fork一个的轻量级进程,于是就想验证一下(笔者的机器是Linux的).当时用top命令的时候,进程总是 ...

  6. Effective Java 读书笔记之六 方法

    一.检查参数的有效性 1.考虑参数有哪些限制,把限制写到文档中,在方法的开头处通过显式地检查来实施这些限制. 二.必要时进行保护性拷贝 1.如果类具有从客户端得到或者返回的可变组件,类就必须考虑保护性 ...

  7. CSS 继承深度解析

    FROM ME: 之前在研究前端性能优化的时候,就有学习关于CSS中“善用CSS中的继承”. 原文:CSS Inheritance, The Cascade And Global Scope: You ...

  8. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  9. Linux之匹配符

    shell常见通配符: 字符 含义 实例 * 匹配 0 或多个字符 a*b  a与b之间可以有任意长度的任意字符, 也可以一个也没有, 如aabcb, axyzb, a012b, ab. ? 匹配任意 ...

  10. 解决vista和win7在windows服务中交互桌面权限问题:穿透Session 0 隔离

        在某国外大型汽车公司BI项目中,有一个子项目,需要通过大屏幕展示销售报表,程序需要自动启动和关闭.开发人员在开发过程中,发现在Win7的service中不能直接操作UI进程,调查过程中,发现如 ...