# reverse linked list II

Reverse a linked list from positionmton. Do it in-place and in one-pass.

For example:\
Given`1->2->3->4->5->NULL`,m= 2 andn= 4,

return`1->4->3->2->5->NULL`.

**Note:**\
Givenm,nsatisfy the following condition:\
1 ≤m≤n≤ length of list.

分析

2种reverse方法，一种只改变中间指针指向，还有一种不断把next插入prev的后面，头插法？也是循环首位相接

```
ListNode temp = cur.next;
cur.next = prev;
prev = cur;
cur = temp;
```

```
头插法？不断把cur后面的Node插入prev的后面
ListNode move = cur.next;
cur.next = move.next;
move.next = prev.next;
prev.next = move;
```

此题从dummy开始，走m-1步到m前，然后开始头插法把后面的数不断插入m-1之后。

```
/**
 * 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) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy, cur;
        for(int i = 0; i < m - 1; i ++){
            if(prev != null)
                prev = prev.next;
        }
        cur = prev.next;
        for(int i = m; i < n; i ++){
            ListNode move = cur.next;
            cur.next = move.next;
            move.next = prev.next;
            prev.next = move;
        }


        return dummy.next;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/l6lian-biao/reverse-linked-list-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
