mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
40 lines
685 B
Go
40 lines
685 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// ListNode define
|
|
type ListNode = structures.ListNode
|
|
|
|
/**
|
|
* Definition for singly-linked list.
|
|
* type ListNode struct {
|
|
* Val int
|
|
* Next *ListNode
|
|
* }
|
|
*/
|
|
func rotateRight(head *ListNode, k int) *ListNode {
|
|
if head == nil || head.Next == nil || k == 0 {
|
|
return head
|
|
}
|
|
newHead := &ListNode{Val: 0, Next: head}
|
|
len := 0
|
|
cur := newHead
|
|
for cur.Next != nil {
|
|
len++
|
|
cur = cur.Next
|
|
}
|
|
if (k % len) == 0 {
|
|
return head
|
|
}
|
|
cur.Next = head
|
|
cur = newHead
|
|
for i := len - k%len; i > 0; i-- {
|
|
cur = cur.Next
|
|
}
|
|
res := &ListNode{Val: 0, Next: cur.Next}
|
|
cur.Next = nil
|
|
return res.Next
|
|
}
|