详情看代码
/**

  • struct ListNode {
  • int val;
  • struct ListNode *next;
  • };
  • /

class Solution {
public:
/*
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
ListNode
reverseBetween(ListNode* head, int m, int n) {
// write code here
int count=0;
ListNodeH=new ListNode(-1);
ListNode
R=H;
ListNodeP=NULL;
ListNode
C=NULL;
while(head)
{
//计数
count++;
//将[m,n]区间的链表进行反转
if(count>=m&&count<=n)
{
ListNode*c;
while(count<=n)
{
P=head;
head=head->next;
P->next=C;
C=P;
//记录反转的尾节点
if(count==m)
{
c=C;
}
count++;
}
H->next=C;
//将H更新为c
H=c;
}
//非反转区间
else
{
H->next=head;
head=head->next;
H=H->next;
}
}
return R->next;
}
};