Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
if(head==NULL||head->next==NULL)
return head;
int i,j;
struct ListNode *p,*cur,*q;
struct ListNode *hhead=(struct ListNode *)malloc(sizeof(struct ListNode));
hhead->next=head;
p=hhead;
for(i=1;i<m;i++)
p=p->next;
cur=p->next;
for(i=m;i<n;i++)
{
q=cur->next;
cur->next=q->next;
q->next=p->next;
p->next=q;
}
return hhead->next;
}