25. Reverse Nodes in k-Group

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.

k is a positive integer and is less than or equal to the length of the linked 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* reverseList(ListNode* first, ListNode* tail)
    {
        ListNode* pre = NULL;
        ListNode* cur = first;
        ListNode* next = NULL;
        while (cur != tail)
        {
            next = cur->next;
            cur->next = pre; //第一个节点就为空啊!!!

            pre = cur;
            cur = next;
        }
        return first;
    }
    ListNode* reverseKGroup(ListNode* head, int k) {

        if (k < 2 || !head)
        {
            return head;
        }
        ListNode* tail = NULL;
        ListNode* first = head;

        ListNode* cur = head;
        ListNode* newHead = NULL;

        ListNode* ret = NULL;
        ListNode* pre = NULL;

        int cnt = k;
        while (cur)
        {

            while (cur&&cnt--)
            {
                /*if (cur== NULL)
                {
                break;
                }*/
                pre = cur; //
                cur = cur->next;
            }
            if (cnt > 0)
            {
                break;
            }
            if (ret)
            {
                ret->next = pre; // 反转链表的尾节点直接挂下一轮k个节点的尾节点
            }
            ret = reverseList(first, cur); //cur下一次的起点,不动;返回反转后的尾节点

            ret->next = cur; //反转链表的尾节点直接挂下一轮k个节点的开始;防止下一轮没有k个节点
            first = cur;

            cnt = k;
        }

        if (!newHead)
        {
            return head;
        }

        return newHead;
    }
};

    ListNode* reverseKGroup(ListNode* head, int k) {
        if(!head) return NULL;
        ListNode* dummy = new ListNode(0);
        ListNode* cur = head;
        ListNode* pre = NULL;
        ListNode* last_tail = dummy;
        int num = 0;
        while(cur){
            cur = cur->next;
            num++;
        }
        cur = head;
        while(num >= k){
            ListNode* tail = cur;
            for(int i = 0; i < k; i++){
                ListNode* temp = cur->next;
                cur->next = pre;
                pre = cur;
                cur = temp;
            }
            last_tail->next = pre;
            last_tail = tail;
            num -= k;
        }
        last_tail->next = cur;
        return dummy->next;
    }

题目来源

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/8351325.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞