zoukankan      html  css  js  c++  java
  • LeetCode

     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;
    }
    /*

    */
  • 相关阅读:
    【Python】小练习
    【C语言】利用二维数组输出成绩
    【C语言】多维数组
    【C语言】输入一个字符串,并对字符串中的偶数位置的字符按从小到大的顺序排序,奇数位置的字符不动,输出排序后的结果
    【C语言】一堆数组中存放了10个小于100的整数,请编程对所有数据按照从小到大的顺序进行排序,若个位数相等,则按照十位从小到大的顺序排序,输出排序后的结果
    【C语言】移动指针
    Python中68个内置函数的总结
    【Python】变量命名习惯
    【Python】 基础语法
    【Python】 注释
  • 原文地址:https://www.cnblogs.com/crazyacking/p/5200484.html
Copyright © 2011-2022 走看看