# 92. Reverse Linked List II

[Link](https://leetcode.com/problems/reverse-linked-list-ii/)

**Solution**

這題有點要用背的

先建立一個dummy node 其next 指向 head

然後找到要開始轉的node的前一個設定為pre，

pre.next就是start。然後start.next就是then

透過pre, start, then這三個node之間的運作把list 局部reverse 進行(n-m)次

![](https://2628390332-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0RnkuvOeqe0clAIN1b%2F-M59iAwb8rWF_lF0NvYZ%2F-M59jcJLyk5oyPQZ7zOA%2Fimage.png?alt=media\&token=69942878-fc3b-494b-a69e-d419cec339bf)

```
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode pre = dummy;
        for(int i = 1 ; i < m ; i ++){
            pre = pre.next;
        }
        ListNode start = pre.next;
        ListNode then = start.next;
        for(int i = 0; i < (n-m); i++){
            start.next = then.next;
            then.next = pre.next;
            pre.next = then;
            then = start.next;
        }
        return dummy.next;
    }
```
