struct ListNode* reverseBetween(struct ListNode* head, int m, int n ) {
if(head->next==NULL||head==NULL)
return head;
struct ListNode* H=malloc(sizeof(struct ListNode));
H->next=head;
struct ListNode *p,*temp,*cur=H;
for(int i=0;i<m-1;i++)
{
cur=cur->next;
}
temp=cur;
struct ListNode* q;
cur=cur->next;
q=cur;
for(int i=0;i<n-m+1;i++)
{
p=cur;
cur=cur->next;
p->next=temp->next;
temp->next=p;
}
q->next=cur;
return H->next;
}
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head->next==nullptr||head==nullptr)
return head;
ListNode *H=new ListNode(0);
ListNode *cur=H;
H->next=head;
for(int i=0;i<m-1;i++)
{
cur=cur->next;
}
ListNode *NewHead=cur;
ListNode *q=cur->next;
cur=cur->next;
ListNode *temp;
for(int i=0;i<n-m+1;i++)
{
temp=cur;
cur=cur->next;
temp->next=NewHead->next;
NewHead->next=temp;
}
q->next=cur;
return H->next;
}
};