classSolution: defrotateRight(self, head: ListNode, k: int) -> ListNode: if k == 0ornot head ornot head.next: return head n = 1 cur = head while cur.next: cur = cur.next n += 1 if (add := n - k % n) == n: return head cur.next = head while add: cur = cur.next add -= 1 ret = cur.next cur.next = None return ret
var rotateRight = function(head, k) { if (k === 0 || !head || !head.next) { return head; } let n = 1; let cur = head; while (cur.next) { cur = cur.next; n++; }
let add = n - k % n; if (add === n) { return head; }
cur.next = head; while (add) { cur = cur.next; add--; }
const ret = cur.next; cur.next = null; return ret; };
funcrotateRight(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 }