zoukankan      html  css  js  c++  java
  • [leetcode]Reverse Nodes in k-Group

    用递归就比较简单。

    class Solution {
    public:
        ListNode *reverseKGroup(ListNode *head, int k) {
            ListNode* current = head;
            ListNode** nodeArray = new ListNode*[k];
            int i = 0;
            while (current != NULL && i < k)
            {
                nodeArray[i] = current;
                current = current->next;
                i++;
            }
            if (i < k) return head;
            
            for (int j = k-1; j > 0; j--)
            {
                nodeArray[j]->next = nodeArray[j-1];
            }
            nodeArray[0]->next = reverseKGroup(current, k);
            return nodeArray[k-1];
        }
    };
    

     但是这样我用了O(n)的空间,虽然是指针的空间。其实不必,因为总是下一个的next指向上一个,循环就行了。看参考答案如下:

    Analysis:
    The idea is to scan from the 1st node to the last, if there are k nodes, then swap them, and count again, until get the end.
    In order to swap k nodes, we can have
    (1) The previous node, to link the previous list. (pre)
    (2) The 1st node to swap. (st)
    (3) The kth node to swap. (ed)
    The idea is to insert the current 1st node behind the end node.
    e.g.
    1->2->3->4->null, when k=4
    (1)2->3->4->1->null
    ed
    (2)3->4->2->1->null
    ed
    (3)4->3->2->1->null
    ed
    After this swap, do not forget link the swapped list to the previous list.

    class Solution {
    public:
        ListNode *reverseKGroup(ListNode *head, int k) {
            ListNode* p=head;
            ListNode* pre=new ListNode(0);
            pre->next=p;
            head = pre;
            int c=0;
            ListNode* st;
            ListNode* ed;
             
            while(p!=NULL){
                c++;
                if (c==1){st = p;} //get start node of every k nodes
                if (c==k){
                    ed = p;         // get end node of every k nodes
                    ListNode *last=ed;  //store the list after the k nodes
                    ListNode *nst=st;   //store the next node to be reversed
                    while (st!=ed){     // reverse the k nodes
                        last = ed->next;
                        nst = st->next;
                        st->next = last;
                        ed->next = st;
                        st=nst;
                    }
                    pre->next = st;     //link to the previous list
                    for (int i=0;i<k-1;i++){    //get the end of the k nodes
                        p=p->next;    
                    }
                    c=0;                //reset count = 0 
                }
                if (c==0){pre = p;}     //store the previous list
                p=p->next;              //go next nodes
            }
            return head->next;
        }
    };
    

      

      

  • 相关阅读:
    第一章:Android系统的编译和移植实例读书笔记
    第二章:Android系统与嵌入式开发读书笔记
    第三章:Android移植平台工具介绍读书笔记
    第十章 嵌入式Linux的调试技术
    第九章 硬件抽象层:HAL
    第八章 让开发板发出声音:蜂鸣器驱动
    第七章 LED将为我闪烁:控制发光二极管
    第六章 第一个Linux驱动程序:统计单词个数
    第五章 搭建S3C6410开发板的测试环境
    第四章 源代码的下载和编译
  • 原文地址:https://www.cnblogs.com/lautsie/p/3309020.html
Copyright © 2011-2022 走看看