zoukankan      html  css  js  c++  java
  • 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)

    Reverse Nodes in k-Group

    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

    解法一:

    看到逆序,第一反应就是栈。

    使用栈,每k个结点进栈,再出栈,就实现了逆序。

    /**
     * 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) {
            ListNode* newhead = new ListNode(-1);
            ListNode* tail = newhead;
            ListNode* begin = head;
            ListNode* end = begin;
            while(true)
            {
                int count = k;
                while(count && end != NULL)
                {
                    end = end->next;
                    count --;
                }
                if(count == 0)
                {//reverse from [begin, end)
                    stack<ListNode*> s;
                    while(begin != end)
                    {
                        s.push(begin);
                        begin = begin->next;
                    }
                    while(!s.empty())
                    {
                        ListNode* top = s.top();
                        s.pop();
                        tail->next = top;
                        tail = tail->next;
                    }
                }
                else
                {//leave out
                    tail->next = begin;
                    break;
                }
            }
            return newhead->next;
        }
    };

    解法二:

    自定义函数reverse(begin, end)

    对[begin, end]范围内实现逆序,并且更新begin, end

    逐k次调用即可。

    注意:

    (1)由于需要更新begin, end,因此参数形式为ListNode*&

    (2)在[begin,end]范围内实现逆序之后,需要链如原先的链表,不可脱离

    /**
     * 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) {
            if(head == NULL)
                return NULL;
            if(k == 1)
            //no swap
                return head;
                
            int i = 1;
            //head node
            ListNode* newhead = new ListNode(-1);
            newhead->next = head;
            ListNode* tail = newhead;
            
            ListNode* begin = head;
            ListNode* end = begin;
            while(end != NULL)
            {
                if(i%k == 0)
                {
                    reverse(begin, end);
                    tail->next = begin;
                    tail = end;
                    //new begin
                    begin = end->next;
                }
                end = end->next;
                i ++;
            }
            return newhead->next;
        }
        void reverse(ListNode*& begin, ListNode*& end)
        {//reverse the list. begin points to new begin, end points to new end
            if(begin == end)
            {//only one node
                return;
            }
            else if(begin->next == end)
            {//two nodes
                begin->next = end->next;
                end->next = begin;
                //swap begin and end
                ListNode* temp = begin;
                begin = end;
                end = temp;
            }
            else
            {//at least three nodes
                ListNode* pre = begin;
                ListNode* cur = pre->next;
                ListNode* post = cur->next;
                
                while(post != end->next)
                {
                    cur->next = pre;
                    pre = cur;
                    cur = post;
                    post = post->next;
                }
                cur->next = pre;
                //old begin points to the new end
                end = begin;
                end->next = post;
                //cur points to the old end
                begin = cur;
            }
        }
    };

  • 相关阅读:
    HTML5简介
    PHP
    纯CSS3写的10个不同的酷炫图片遮罩层效果
    零基础如何自学MySQL数据库?
    js与jQuery
    MAC下GitHub命令操作
    框架基础:ajax设计方案(二)---集成轮询技术
    框架基础:ajax设计方案(一)---集成核心请求
    框架基础:ajax设计方案(三)---集成ajax上传技术
    Jquery操作下拉列表和复选框,自定义下拉
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4174119.html
Copyright © 2011-2022 走看看