25. Reverse Nodes in k-Group

Problem's Link

----------------------------------------------------------------------------

Mean:

给定一个链表和一个k值,将链表按照k个结点为一组,组内翻转.

analyse:

继续抖机灵!

Time complexity: O(N)

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-19-11.06
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);

// 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)
   {
       vector<int> ve;
       ListNode *tptr=head;
       while(head)
       {
           ve.push_back(head->val);
           head=head->next;
       }

if(ve.size()<k) return tptr;
       delete(head);
       int frontIndex=0,backIndex=k-1;
       while(frontIndex<ve.size() && backIndex<ve.size())
       {
           int low=frontIndex,high=backIndex;
           while(low<high)
           {
               swap(ve[low],ve[high]);
               ++low,--high;
           }
           frontIndex=backIndex+1;
           backIndex+=k;
       }

int isFirst=1;
       ListNode *res=nullptr,*p=nullptr;
       for(int i=0; i<ve.size(); ++i)
       {
           if(isFirst)
           {
               isFirst=0;
               p=new ListNode(ve[i]);
               res=p;
           }
           else
           {
               p->next=new ListNode(ve[i]);
               p=p->next;
           }
       }
       return res;
   }
};

int main()
{
   Solution solution;
   int n,k;
   while(cin>>n>>k)
   {
       bool isFirst=1;
       ListNode *res=nullptr,*head=nullptr;
       for(int i=0; i<n; ++i)
       {
           int tmp;
           cin>>tmp;
           if(isFirst)
           {
               isFirst=0;
               head=new ListNode(tmp);
               res=head;
           }
           else
           {
               head->next=new ListNode(tmp);
               head=head->next;
           }
       }
       ListNode *ans=solution.reverseKGroup(res,k);
       while(ans)
       {
           cout<<ans->val<<" ";
           ans=ans->next;
       }
       cout<<"End."<<endl;
   }
   return 0;
}
/*

*/

转载于:https://www.cnblogs.com/crazyacking/p/5200484.html

LeetCode - 25. Reverse Nodes in k-Group相关推荐

  1. 【重点】LeetCode 25. Reverse Nodes in k-Group

    LeetCode 25. Reverse Nodes in k-Group 博客转载自:http://www.cnblogs.com/grandyang/p/4441324.html Solution ...

  2. LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)

    题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分为k ...

  3. leetcode 25. Reverse Nodes in k-Group | 25. K 个一组翻转链表(Java)

    题目 https://leetcode.com/problems/reverse-nodes-in-k-group/ 题解 乍一看以为很容易:每 k 个节点翻转,若剩余不足 k 个,则不变.没有什么技 ...

  4. 25. Reverse Nodes in k-Group

    https://leetcode.com/problems/reverse-nodes-in-k-group/description/ 题意:给一个链表和一个正数k,将链表划分成多个长度为k的链,将这 ...

  5. 【leetcode】25. Reverse Nodes in k-Group 链表按K分段逆序

    1. 题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...

  6. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  7. LeetCode之Reverse String II

    1.题目 Given a string and an integer k, you need to reverse the first k characters for every 2k charac ...

  8. LeetCode 974. 和可被 K 整除的子数组(哈希map)

    1. 题目 给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目. 示例: 输入:A = [4,5,0,-2,-3,1], K = 5 输出:7 解释: 有 7 个子数组满 ...

  9. LeetCode 862. 和至少为 K 的最短子数组(前缀和+deque单调栈)

    1. 题目 返回 A 的最短的非空连续子数组的长度,该子数组的和至少为 K . 如果没有和至少为 K 的非空子数组,返回 -1 . 示例 1: 输入:A = [1], K = 1 输出:1示例 2: ...

最新文章

  1. Postman使用Date数据类型,Postman发送Date类型数据,Postman模拟前端调用
  2. yjv是电缆还是电线_电力电缆YJV与BVV二者之间的区别是什么?
  3. c#NPOI导出2007版本excel
  4. mysql+yes数据类型,怎样修改mysql列的数据类型?
  5. Spring——bean生命周期
  6. 如何降低 Python 的内存消耗量?
  7. android webview video标签,Android WebView支持html5 video标签
  8. 华中农业大学C语言实验5答案,物理实验报告册(上册)-华中农业大学实验.pdf
  9. JPDA 架构研究4 - JDWP的传输器
  10. net.sf.json Sring转JSON对象 数据精度丢失
  11. Springboot 默认加载文件(可直接访问、不可直接访问)是出现的问题
  12. java指定一个具体日期
  13. 清华大学中国人工智能学会:2019人工智能发展报告
  14. 口令破解:kali字典工具的使用:CUPP、Crunch、Hydra
  15. Jmeter脚本录制和压测
  16. java中html网页转化成pdf(itext)
  17. pc恶意程序木马分析 启动流程 逆向分析 数据解密
  18. html在搜索按钮中加放大镜,用 CSS3 画心形和搜索放大镜图标
  19. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.17:test (default-test) on pr
  20. 谷歌智能眼镜再掀数码浪潮

热门文章

  1. 科大讯飞俞仁忠:组织结构与激励机制
  2. SAP MM PR中的Fixed ID字段与MD04里PR单据号后的星号
  3. 脑智前沿科普|虚拟现实如何欺骗你的大脑
  4. 物理如何证明上帝的存在?
  5. 美国半导体十年计划中的NO.1,模拟硬件究竟有什么价值?
  6. 范式变革与规律涌现:世界科技发展新趋势
  7. “万维网之父”发文阐述其下一个网络时代:将数据与应用分离,互联网去中心化正在路上...
  8. 斯坦福大学、DARPA与硅谷公司共同分析前沿科技发展趋势
  9. 18年你需要了解的15个人工智能统计数据
  10. 杨立昆辞Facebook人工智能实验室主任,任首席科学家