---
# System prepended metadata

title: 0061. Rotate List
tags: [Leetcode, Linked List, Medium]

---

# 0061. Rotate List
Link: https://leetcode.com/problems/rotate-list/
###### tags: `Leetcode` `Medium` `Linked List`
## 思路
1. 确定linked list的总长度 注意```len==0```的处理 并闭合成环
2. 确定新的head的位置 是head往后数```len-k%len-1```个的下一个
3. 找到新的head前面的那一个```curr``` 它的next就是```newHead```,```curr.next=null```

## Code
```java=
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(head==null) return head;
        int len = 0;
        ListNode curr = head, end = head;
        while(curr!=null){
            end = curr;
            curr = curr.next;
            len++;
        }
        k = len-k%len-1;
        end.next = head;
        ListNode newHead = null;
        curr = head;
        for(int i=0; i<k; i++){
            curr = curr.next;
        }
        newHead = curr.next;
        curr.next = null;
        return newHead;
    }
}
```
