码迷,mamicode.com
首页 > 其他好文 > 详细

Reverse Linked List II

时间:2015-06-09 10:01:22      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:c++   算法   

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

标签:c++   算法   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!