LeetCode 61 旋转链表

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

示例 1:

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

示例 2:

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

提示:

  • 链表中节点的数目在范围 [0, 500] 内
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

解题:

func rotateRight(head *ListNode, k int) *ListNode {
	if head == nil {
		return nil
	}
	if head.Next == nil {
		return head
	}
	start := head
	l := 0
	for head.Next != nil {
		head = head.Next
		l++
	}
	l++
	head = start
	k %= l
	for i := 0; i < k; i++ {
		for head.Next != nil && head.Next.Next != nil {
			head = head.Next
		}
		head.Next.Next = start
		start = head.Next
		head.Next = nil
		head = start
	}
	return start
}

官方解答:

1.闭合为环

func rotateRight(head *ListNode, k int) *ListNode {
	if k == 0 || head == nil || head.Next == nil {
		return head
	}
	n := 1
	iter := head
	for iter.Next != nil {
		iter = iter.Next
		n++
	}
	add := n - k%n
	if add == n {
		return head
	}
	iter.Next = head
	for add > 0 {
		iter = iter.Next
		add--
	}
	ret := iter.Next
	iter.Next = nil
	return ret
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注