package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
type ListNode = structures.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
}