> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/chapter_4_linked_list/reverse-linked-list/problem_92_reverse_linked_list_ii.md).

# Problem 92: Reverse Linked List II

> <https://leetcode.com/problems/reverse-linked-list-ii/>

## 思路

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvPtqseIA8TO6aG%2FReverseList_II.jpg?generation=1569729533428111\&alt=media)\
1\. 找到四个关键点\
2\. m 到 n 反转\
3\. 拼接

```java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if (head == null || m >= n) return head;

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        for (int i = 1; i < m; i++) {
            if (head == null) return null;
            head = head.next;
        }

        ListNode premNode = head;
        ListNode mNode = head.next;
        ListNode nNode = mNode;
        ListNode postNode = nNode.next;
        for (int i = m; i < n; i++) {
            if (postNode == null) return null;

            ListNode tmp = postNode.next;
            postNode.next = nNode;
            nNode = postNode;
            postNode = tmp;
        }
        mNode.next = postNode;
        premNode.next = nNode;

        return dummy.next;
    }
}
```

## 易错点

1. 建立dummy node

   ```java
   ListNode dummy = new ListNode(0);
   dummy.next = head;
   head = dummy;
   ```
2. 移动到 m 点处

   ```java
   for (int i = 1; i < m; i++) {
        if (head == null) {
            return null;
        }
        head = head.next;
   }
   ```

   注意：这里是从 1 到 m，一共是 （m - 1）次移动。
3. reverse

   ```java
   ListNode temp = postnNode.next;
   postnNode.next = nNode;
   nNode = postnNode;
   postnNode = temp;
   ```

   ![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvRke-1GgY14rM1%2Freverse_01.jpg?generation=1569729533374315\&alt=media)

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvTMbvLulwv6hxU%2Freverse_02.jpg?generation=1569729532927537\&alt=media)

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvVO5EmZxiLCVhQ%2Freverse_03.jpg?generation=1569729533164524\&alt=media)\
![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvXBOx8hx7e8Sba%2Freverse_04.jpg?generation=1569729532773034\&alt=media)

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvZp8CXpBMRUIz9%2Freverse_05.jpg?generation=1569729533114356\&alt=media)

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvasg3mgQTEwsKi%2Freverse_06.jpg?generation=1569729533492286\&alt=media)

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9vvcHjhRqMLY1B0R%2Freverse_07.jpg?generation=1569729532797436\&alt=media)
