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

  • 相关阅读:
    《自拍教程17》Python调用命令
    c和c++学哪个?
    PHP:变量之效果域、静态变量,常量等基础知识
    Java中NIO及基础实现
    零代码=零门槛?
    程序员真的都比较宅吗?
    DataGridView怎样完成添加、删除、上移、下移一行
    C# 控件 RichTextBox 显示行号,而且与Panel彼此联动
    C语言代码中的空白符表示什么
    php 中的4种标记风格介绍
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4174119.html
Copyright © 2011-2022 走看看