ListNode *reverseBetween(ListNode* head, int m, int n) { if (m == n) return head; n -= m; ListNode prehead(0); prehead.next = head; ListNode *pRevBeginPrev = &prehead; while (--m) pRevBeginPrev = pRevBeginPrev->next; ListNode *pRevBegin = pRevBeginPrev->next; ListNode *pCur = pRevBegin, *pPrev = NULL, *pNext; while (n--) { pNext = pCur->next; pCur->next = pPrev; pPrev = pCur; pCur = pNext; } pRevBegin->next = pCur->next; pCur->next = pPrev; pRevBeginPrev->next = pCur; return prehead.next; }
原文地址:http://blog.csdn.net/liuxinglin/article/details/46418799