zoukankan      html  css  js  c++  java
  • LeetCode(25)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

    分析

    该题目核心是分组对一个链表进行反转,链接,如示例所示。

    AC代码

    /**
     * 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 *p = head;
            int len = 0;
            while (p)
            {
                len++;
                p = p->next;
            }
            if (len < k || k == 1)
                return head;
    
            vector<ListNode *> vl;
            ListNode *h = head;
    
            while (len >= k)
            {
                //找到len/k个子链表,分别翻转
                ListNode *q = h;
                for (int i = 1; i < k; i++)
                    q = q->next;
    
                //保存后续结点
                ListNode *r = q->next;
    
                q->next = NULL;
    
                vl.push_back(reverse(h));
    
                h = r;
                len -= k;
            }//while
    
            //将翻转的链表链接起来,保存剩余的小于k个的结点
            ListNode *re = h;
            if (vl.size() != 1)
            {
    
                for (int i = 0; i < vl.size() - 1; i++)
                {
                    p = LastNode(vl[i]);
                    p->next = vl[i + 1];
                }//for
                p->next = vl[vl.size() - 1];
            }
    
            LastNode(vl[vl.size() - 1])->next = re;
    
            head = vl[0];
            return head;
    
        }
    
        ListNode *LastNode(ListNode *head)
        {
            while (head && head->next)
                head = head->next;
            return head;
        }
    
        ListNode* reverse(ListNode* head)
        {
            if (head == NULL || head->next == NULL)
                return head;
    
            ListNode *ret = head , *p = head->next;
            ret->next = NULL;
    
            while (p)
            {
                //保存下一个结点
                ListNode *q = p->next;
    
                p->next = ret;
                ret = p;
                p = q;
            }
            return ret;
        }
    };

    GitHub测试程序源码

  • 相关阅读:
    前端总结(设计向)
    bootstrap 样式规范总结
    angular2学习---模板学习
    angular2学习 -- 基本配置学习
    前端相关小技巧以及问题总结
    认识hasLayout——IE浏览器css bug的一大罪恶根源 转
    bug 由于浏览器缓存而引起的ajax请求并没有获取到服务器最新数据从而导致的bug
    总结 好用的工具/网站/插件
    .NET FrameWork完全卸载
    ASP.NET4.0项目部署到Win7系统的IIS上
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214918.html
Copyright © 2011-2022 走看看