LeetCode-25: Reverse Nodes in k-Group (reversing the linked list in k-sized groups)

topic:

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.

example:

Example 1:

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

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list’s nodes, only nodes itself may be changed.

Problem analysis:

Given a singly linked list, flip the linked list with k nodes as a group. If the remaining nodes are less than k, no flipping is performed.

Link:

ideas label

Step-by-step : first count, then flip

answer:

  • The problem is actually not difficult, the key is to understand the operation of flipping and the required nodes;
  • First, count whether the following nodes satisfy k to be flipped as a group, if not, it will not be flipped;
  • Note that each time you need to record the last node of the previous flip group, and then connect with the flip list of the next group;
  • Note that the head node of the returned linked list is the head node that records the first group of flipped head nodes, which has nothing to do with the following ones, or if no group has been flipped, the original linked list head node is directly returned.
/**
 * 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 (k == 1 || head == nullptr)
            return head;

        ListNode *pHead = head;
        ListNode *pNode = head;
        ListNode *pTailPrevious = pNode;
        int first = true;
        while(pNode != nullptr){
            ListNode *pTail = pNode;

            int n = 1;
            // 统计后面是否还存在k个节点
            while(pNode != nullptr && n <= k){
                pNode = pNode->next;
                n++;
            }

            // 如果还存在k个节点则进行翻转并对该k个节点翻转链表与之前的进行连接,否则不进行翻转直接连接。
            if(n > k){
                pNode = pTail;
                ListNode *pPrevious = nullptr;
                n = 1;
                while(n <= k){
                    ListNode *pNext = pNode->next;
                    pNode->next = pPrevious;
                    pPrevious = pNode;
                    pNode = pNext;
                    n++;
                }

                // 判断是否是第一段,第一段的翻转节点为最后的头节点
                if(first == true){
                    pHead = pPrevious;
                    pTailPrevious = pTail;
                    first = false;
                }else{
                    pTailPrevious->next = pPrevious;
                    pTailPrevious = pTail;
                }
            }else{
                // 如果还是第一段则说明不足k个,直接返回链表
                if(first == true){
                    return pHead;
                }
                pTailPrevious->next = pTail;
            }
        }

        return pHead;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809351&siteId=291194637