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

  • 相关阅读:
    leetcode刷题笔记九十八题 验证二叉搜索树
    leetcode刷题笔记九十七题 交错字符串
    leetcode刷题笔记九十六题 不同的二叉搜索树
    leetcode刷题笔记九十五题 不同的二叉搜索树II
    leetcode刷题笔记九十四题 二叉树的中序遍历
    leetcode刷题笔记九十三题 复原IP地址
    java 语句流程
    java byte/short/char补充(了解)
    Java反射机制demo(四)—获取一个类的父类和实现的接口
    AOP方法拦截获取参数上的注解
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4174119.html
Copyright © 2011-2022 走看看