#include <iostream>
using namespace std;
typedef struct ListNode{
int val;
struct ListNode *next;
ListNode(int x):val(x),next(NULL){};
}ListNode;
void printlist(ListNode *p)
{
while(p)
{
cout << p->val <<endl;
p = p->next;
}
}
ListNode* reverseBetween(ListNode* head, int m, int n)
{
if(m==n)
return head;
ListNode*pre =new ListNode(-1);
pre->next=head;
ListNode*prehead=pre;
for(int i=1; i<m; i++)
{
prehead=prehead->next;
}
// printlist(pre);
n-=m;
ListNode* pstart=prehead->next;
//链表反转涉及到四个节点
for(int i=1; i<=n; i++)
{
ListNode* p=pstart->next;
pstart->next=p->next;
p->next=prehead->next;
prehead->next=p;
}
return pre->next;
}
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
ListNode *NewHead = new ListNode(-1);
ListNode *pHead = NewHead;
for(int i=0 ; i<10 ; i++)
{
ListNode *tmp = new ListNode(a[i]);
pHead->next = tmp;
pHead = tmp;
}
ListNode *p1;
p1= reverseBetween(NewHead->next,4,6);
printlist(p1);
return 0;
}