import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode rotateLinkedList (ListNode head, int k) {
//判断链表是否空,k值是否为空
if(head == null || k == 0){
return head;
}
int len = 0 ;
Map<Integer,Integer> map = new HashMap<>();
ListNode temp1 = head;
// 遍历链表,存入map,并得到长度
for(int i=0; temp1 != null; i++){
map.put(i,temp1.val);
temp1 = temp1.next;
len++;
}
k=k%len;
ListNode temp2 = head;
for(int j=0;temp2 != null;j++){
temp2.val = map.get((j+len-k)%len);
temp2 = temp2.next;
}
return head;
}
}