class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null){
return null;
}
ListNode cur = head;
int n = 1;
while(cur.next != null){
cur = cur.next;
n++;
}
cur.next = head;
ListNode helper = head;
int i = 0;
while(i < n - k%n -1){
helper = helper.next;
i++;
}
head = helper.next;
helper.next = null;
return head;
}
}
61. 旋转链表 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。 示例 1: 输入:head = [1,2,3,4,5], k = 2 输出:[4,5,1
最新推荐文章于 2024-05-06 19:37:53 发布