首页 > 其他 > 详细

Reverse Linked List II

时间:2015-06-09 10:01:06      阅读:243      评论:0      收藏:0      [点我收藏+]
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

原文:http://blog.csdn.net/liuxinglin/article/details/46418799

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!