【Reverse Nodes in k-Group】cpp

题目

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

代码:

/**
 * 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 || !(head->next) || k<2 ) return head;
            ListNode dummy(-1);
            dummy.next = head;
            ListNode *beginK = &dummy;
            ListNode *endK = &dummy;
            while ( true )
            {
                // move forward k steps
                for ( size_t i = 0; i < k && endK; ++i ) {endK = endK->next;}
                // check if already move to the end of linked list
                if (!endK) break;
                // reverse from beginK to endK
                ListNode *curr = beginK->next;
                for ( size_t i = 0; i < k-1; ++i )
                {
                    ListNode *tmp = curr->next;
                    curr->next = tmp->next;
                    tmp->next = beginK->next;
                    beginK->next = tmp;
                }
                beginK = curr;
                endK = curr;
            }
            return dummy.next;
    }
};

Tips

这道题是Reverse Linked List(http://www.cnblogs.com/xbf9xbf/p/4464896.html)的加强版。

大概分两步:

1. 判断这一group是否够k个元素

2. 将这一group的k个元素reverse

每次reverse之前,都要构造成如下的条件

结合代码以及下面的示例:以k=3为例

beginK→1(curr)→2→3(endK)→...

beginK:这一group之前的那个ListNode

curr: 这一group的第一个元素

endK:这一group的最后一个元素

3. 注意每次reverse完成后,迭代beginK和endK的值。

4. 注意循环终止的条件是endK移动到的NULL。

代码的风格追求简洁,因为简洁往往方便理解。

=============================================

第二次过这道题,一次AC了。这道题主要考查reverse linked list的操作。

/**
 * 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 || k<2 ) return head;
            ListNode dummpy(-1);
            dummpy.next = head;
            ListNode* p1 = &dummpy;
            ListNode* p2 = &dummpy;
            for ( int i=0; i<k && p2; ++i ) p2 = p2->next;
            while ( p2 )
            {
                // reverse Nodes in one group
                ListNode* curr = p1->next;
                for ( int i=0; i<k-1; ++i )
                {
                    ListNode* tmp = curr->next;
                    curr->next = tmp->next;
                    tmp->next = p1->next;
                    p1->next = tmp;
                }
                p1 = curr;
                p2 = curr;
                // if existing next k-group Nodes
                for ( int i=0; i<k && p2; ++i ) p2 = p2->next;
            }
            return dummpy.next;
    }
};
原文地址:https://www.cnblogs.com/xbf9xbf/p/4468268.html