首页 > 其他 > 详细

***25 k个一组反转链表

时间:2018-12-25 18:46:30      阅读:192      评论:0      收藏:0      [点我收藏+]
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (head == nullptr || head->next == nullptr || k == 1)
            return head;
        int num = 0;
        ListNode *preheader = new ListNode(-1);
        preheader->next = head;
        ListNode *cur = preheader, *nex, *pre = preheader;
        while ((cur = cur->next))
            num++;
        while (num >= k) {
            cur = pre->next;
            nex = cur->next;
            for (int i = 1; i < k; ++i) {
                cur->next = nex->next;
                nex->next = pre->next;
                pre->next = nex;
                nex = cur->next;
            }
            pre = cur;
            num -= k;
        }
        return preheader->next;
    }

***25 k个一组反转链表

原文:https://www.cnblogs.com/INnoVationv2/p/10175479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!