# LeetCode 25. K a group of flip list

Keywords: Java github less network

# subject

I'll give you a list. Every k nodes will be flipped. Please return to the flipped list.

k is a positive integer whose value is less than or equal to the length of the list.

If the total number of nodes is not an integral multiple of k, keep the last remaining nodes in the same order.

Example:

Here is the list: 1 - > 2 - > 3 - > 4 - > 5

When k = 2, it should return: 2 - > 1 - > 4 - > 3 - > 5

When k = 3, you should return: 3 - > 2 - > 1 - > 4 - > 5


explain:

• Your algorithm can only use constant extra space.
• You can't just change the internal values of nodes, but you need to actually exchange nodes.

Source: LeetCode

## Solutions to problems

### Train of thought 1: first, it is divided into k segments, each segment is turned over and then spliced back

Firstly, every k nodes of the original node are divided, each sub segment is flipped, and then the stitching is processed;

Algorithm complexity:

• Time complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$
• Spatial complexity: ${\ color{Magenta}{\Omicron\left(k\right)}}}$

### Train of thought 2 - direct recursive processing of segmentation and segmentation flipping

Each k segment records and recursively, and recursively flips and splices from the last K segments upward. The node processing is relatively winding;

Algorithm complexity:

• Time complexity: ${color{Magenta}{Omicronleft (nright)}}$
• Spatial complexity: ${\ color{Magenta}{\Omicron\left(n\right)}}}$

## Algorithm source code example

package leetcode;

/**
* @author ZhouJie
* @date 2020 10:25:53 PM, January 13, 2010
* @Description: 25. K A set of flip list
*
*/
public class LeetCode_0025 {

}

class ListNode_0025 {
int val;
ListNode_0025 next;

ListNode_0025(int x) {
val = x;
}
}

class Solution_0025 {
/**
* @author: ZhouJie
* @date: 2020 3:15:23 PM, February 4, 2015
* @param: @param k
* @param: @return
* @return: ListNode_0025
* @Description: 1-Iteration;
*
*/
public ListNode_0025 reverseKGroup(ListNode_0025 head, int k) {
p1 = p2 = p3 = newHead = null;
int n = 1;
ListNode_0025 dummy = new ListNode_0025(0);
while (head != null && n < k) {
n++;
}
if (n < k || head == null) {
break;
}
reverse(null, dummy.next);
} else {
p1.next = p2;
}
p1 = dummy.next;
p1.next = p3;
n = 1;
}
}

/**
* @author: ZhouJie
* @date: 2020 3:20:04 PM, February 4, 2004
* @param: @param tail
* @param: @return
* @return: ListNode_0025
* @Description: Flip list
*
*/
private ListNode_0025 reverse(ListNode_0025 tail, ListNode_0025 head) {
return tail;
}
}

/**
* @author: ZhouJie
* @date: 2020 3:26:30 PM, February 4, 2010
* @param: @param k
* @param: @return
* @return: ListNode_0025
* @Description: 2-Recursion;
*
*/
public ListNode_0025 reverseKGroup_2(ListNode_0025 head, int k) {
int count = 0;
while (now != null && count < k) {
now = now.next;
count++;
}
if (count == k) {
now = reverseKGroup_2(now, k);
// head is the last first node of k consecutive nodes, and now is the next node of the last tail node
while (count-- > 0) {