# 61. Rotate List

[Link](https://leetcode.com/problems/rotate-list/submissions/)

**Solution**

先計算list長度: len，然後將最後一個元素(rear)接回最前面的元素(head)

已知 rotate k 次之後， (len- (k%len)的元素 lastNode會變成最後面， lastNode的下一個會變成最前面

```
 public ListNode rotateRight(ListNode head, int k) {
        if (head==null||head.next==null) return head;
        //calculate the len
        int len = 0;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode rear = dummy;
        ListNode lastNode = dummy;
        
        while(rear.next != null){
            rear = rear.next;
            len++;
        }
        rear.next = dummy.next;
        for(int i = 0; i < (len - (k%len)); i++){
            lastNode = lastNode.next;
        }
        dummy.next = lastNode.next;
        lastNode.next = null;
        
        return dummy.next;
    }
```
