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;
}
Reverse Linked List II
最新推荐文章于 2024-08-15 05:02:54 发布