One question per day (3) - a group of K flipped linked lists

The title of this article comes from leetcode

foreword

Hello, everyone, I am Xiaoyu, and today I have completed a difficult problem related to linked lists: a group of K flipped linked lists.

Question

Give you the head node of the linked list head, flip keach node in a group, please return the modified linked list.

kis a positive integer whose value is less than or equal to the length of the linked list. If the total number of nodes is knot an integer multiple of , please keep the last remaining nodes in the original order.

You can't just change the value inside the node, but need to actually swap the node.

Example 1:

输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]

hint:

  • The number of nodes in the linked list isn
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000

Solution

Seeing this question, the first reaction is the most intuitive simulation method. First, write an auxiliary function to flip the linked list, and then continue to find the interval that needs to be flipped and flip it. Among them, it is necessary to pay attention to the nodes in the first half and the second half of the flip interval. At the same time, in order to return the correct result node, a flag is also set to identify whether it is the first flip. If it is the first flip, then the head node needs to be set. The specific code is as follows:

Code

type ListNode struct {
	Val  int
	Next *ListNode
}
func reverseKGroup(head *ListNode, k int) *ListNode {
	var result *ListNode
	var resultflag bool
	var reverse func(head *ListNode, tail *ListNode) *ListNode
	//只翻转head到tail的节点,前后节点并未处理
	reverse = func(head *ListNode, tail *ListNode) *ListNode {
		var pre *ListNode
		now := head
		for pre != tail {
			temp := now.Next
			now.Next = pre
			pre = now
            now = temp
		}
		return pre
	}

	tempnode := head
	var pre *ListNode
	var start *ListNode
	var end *ListNode

	for tempnode != nil {
		flag := true
		start, end = tempnode, tempnode
		for i := 0; i < k-1; i++ {
			end = end.Next
			if end == nil {
				flag = false
				break
			}
		}
		if !flag {
			if !resultflag {
				result = start
			}
            pre.Next = start
			return result
		} else {
			tempnode = end.Next
			node := reverse(start, end)
			if resultflag {
				pre.Next = node
				pre = start
			} else {
				result = node
				resultflag = true
				pre = start
			}

		}

	}

	return result

}

Summarize

When doing linked list questions, don't be afraid of too many variables! Define a few more variables so that it is not easy to make mistakes when writing logic. For example, in this question, use the tempnode variable to identify which node is currently progressing, use the start and end variables to identify the pre-flip interval, and also use the pre To identify the previous node, in fact, the identification inside is somewhat redundant, but the increase in code readability is obvious to all.

Guess you like

Origin blog.csdn.net/doreen211/article/details/129334774