LeetCode 25. K a group of flip list

Keywords: Java github less network

My LeetCode: https://leetcode-cn.com/u/ituring/

My LeetCode source code [GitHub]: https://github.com/izhoujie/Algorithmcii

LeetCode 25. K a group of flip list

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
Link: https://leetcode-cn.com/problems/reverse-nodes-in-k-group
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.

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 {

}

// Definition for singly-linked list.
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 head
	 * @param: @param k
	 * @param: @return
	 * @return: ListNode_0025
	 * @Description: 1-Iteration;
	 *
	 */
	public ListNode_0025 reverseKGroup(ListNode_0025 head, int k) {
		ListNode_0025 p1, p2, p3, newHead;
		p1 = p2 = p3 = newHead = null;
		int n = 1;
		ListNode_0025 dummy = new ListNode_0025(0);
		dummy.next = head;
		while (head != null) {
			dummy.next = head;
			while (head != null && n < k) {
				head = head.next;
				n++;
			}
			if (n < k || head == null) {
				break;
			}
			p2 = head;
			p3 = head.next;
			head.next = null;
			// Flip a link list
			reverse(null, dummy.next);
			if (newHead == null) {
				newHead = p2;
			} else {
				p1.next = p2;
			}
			p1 = dummy.next;
			p1.next = p3;
			head = p3;
			n = 1;
		}
		return newHead == null ? dummy.next : newHead;
	}

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

	/**
	 * @author: ZhouJie
	 * @date: 2020 3:26:30 PM, February 4, 2010 
	 * @param: @param head
	 * @param: @param k
	 * @param: @return
	 * @return: ListNode_0025
	 * @Description: 2-Recursion;
	 *
	 */
	public ListNode_0025 reverseKGroup_2(ListNode_0025 head, int k) {
		ListNode_0025 now = head;
		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) {
				ListNode_0025 temp = head.next;
				head.next = now;
				now = head;
				head = temp;
			}
			// Return the header node after the current segment is processed
			return now;
		}
		return head;
	}
}

Posted by didijeeeke on Sat, 16 May 2020 08:08:27 -0700