# 题目：Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

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

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

# 代码：

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
/*ListNode *dummy=new ListNode(0);
ListNode *slow = dummy;
n -= m;
while (--m)
slow = slow->next;
ListNode *fast = slow->next, *tmp;
while (n--) {
tmp = fast->next;
fast->next = fast->next->next;
tmp->next = slow->next;
slow->next = tmp;
}
return dummy->next;*/

ListNode *dummy=new ListNode(0);

ListNode *slow=dummy;
n=n-m;//这一句话不能够放在下面循环的后面，因为m变了。
while(--m){
slow=slow->next;//在m个之前的一个
}

ListNode *fast=slow->next,*tmp;
while(n--){
tmp=fast->next;
fast->next=fast->next->next;

tmp->next= slow->next;
slow->next=tmp;
//fast=slow->next;
}
return dummy->next;
}
};

#### Reverse Linked List II -- LeetCode

2014-04-28 04:58:33

#### 【LeetCode】Reverse Linked List II 解题报告

2014-12-05 15:59:28

2014-01-28 11:06:09

#### 【LeetCode】92. Reverse Linked List II 解题报告

2017-03-28 09:42:23

#### leetcode：92. Reverse Linked List II（Java）解答

2015-12-26 16:14:34

2014-01-28 01:06:19

2015-03-28 11:17:21

2016-02-19 13:39:20

#### Leetcode Reverse Linked List II 反转特定区间的链表

2013-12-29 08:02:27