给你一个链表的头节点head,旋转链表,将链表每个节点向右移动 k个位置。
输入:head = [1,2,3,4,5], k = 2输出:[4,5,1,2,3]
var rotateRight = function (head, k) {if (!head || !head.next || !k) return headlet n = 1let cur = headwhile (cur.next) {n++cur = cur.next}// 取余,防止多次旋转let add = n - k % n// 建立环形cur.next = headwhile (add) {add--cur = cur.next}const res = cur.next// 切断环形链接cur.next = nullreturn res}
