zoukankan      html  css  js  c++  java
  • [算法练习] 反转链表

    题目说明:

    输入一个链表的头结点,反转该链表,并返回反转后链表的头结点。链表结点定义如下:
    struct ListNode
    {
         int m_nKey;
         ListNode* m_pNext;
    };

     

    程序代码:

    #include <gtest/gtest.h>
    #include <vector>
    using namespace std;
    
    struct ListNode
    {
        int m_nKey;
        ListNode* m_pNext;
    };
    
    ListNode* ReverseLinkedList(ListNode* pHead)
    {
        ListNode* pNext = pHead;    
        ListNode* pPrev = NULL;
        
        while (pNext)
        {
            ListNode* pTemp = pNext->m_pNext;
            pNext->m_pNext = pPrev;
            pPrev = pNext;
            pNext = pTemp;
        }
    
        return pPrev;
    }
    
    // 递归方式实现
    ListNode* ReverseLinkedList2(ListNode* pHead, ListNode* pPrev = NULL)
    {        
        ListNode* pReversedHead = pHead;        
        if (pHead)
        {        
            if (pHead->m_pNext)
            {
                pReversedHead = ReverseLinkedList2(pHead->m_pNext, pHead);
            }
    
            pHead->m_pNext = pPrev;        
        }
    
        return pReversedHead;
    }
    
    ListNode* CreateLinkedList(const string& data)
    {
        ListNode* pHead = NULL;
        ListNode* pLast = NULL;
        for (int i=0; i<data.size(); ++i)
        {
            ListNode* pNext = new ListNode();        
            pNext->m_nKey = data[i];
            pNext->m_pNext = NULL;
    
            if (!pHead)
            {
                pHead = pLast = pNext;
            }
            else
            {
                pLast->m_pNext = pNext;
                pLast = pNext;
            }
        }
    
        return pHead;
    }
    
    void ReleaseLinkedList(ListNode* pHead)
    {
        ListNode* pTemp = NULL;
        while (pHead)
        {
            pTemp = pHead;
            pHead = pHead->m_pNext;
            delete pTemp;
        }
    }
    
    string ConvertLinkedList(ListNode* pHead)
    {
        string strValue;
        while (pHead)
        {
            strValue+= (char)pHead->m_nKey;
            pHead = pHead->m_pNext;
        }
    
        return strValue;
    }
    
    TEST(Pratices, tReverseLinkedList)
    {
        // NULL
        ListNode* pHead = NULL;
        pHead = ReverseLinkedList(pHead);
        ASSERT_TRUE(!pHead);
    
        // a -> a
        pHead = CreateLinkedList("a");
        pHead = ReverseLinkedList(pHead);
        ASSERT_EQ(ConvertLinkedList(pHead),"a");
        ReleaseLinkedList(pHead);
    
        // ab -> ba
        pHead = CreateLinkedList("ab");
        pHead = ReverseLinkedList(pHead);
        ASSERT_EQ(ConvertLinkedList(pHead),"ba");
        ReleaseLinkedList(pHead);
    
        // abcedfg -> gfdecba
        pHead = CreateLinkedList("abcedfg");
        pHead = ReverseLinkedList(pHead);
        ASSERT_EQ(ConvertLinkedList(pHead),"gfdecba");
        ReleaseLinkedList(pHead);
    
        /// 递归逆序测试
        pHead = NULL;
        pHead = ReverseLinkedList2(pHead);
        ASSERT_TRUE(!pHead);
    
        // a -> a
        pHead = CreateLinkedList("a");
        pHead = ReverseLinkedList2(pHead);
        ASSERT_EQ(ConvertLinkedList(pHead),"a");
        ReleaseLinkedList(pHead);
    
        // ab -> ba
        pHead = CreateLinkedList("ab");
        pHead = ReverseLinkedList2(pHead);
        ASSERT_EQ(ConvertLinkedList(pHead),"ba");
        ReleaseLinkedList(pHead);
    
        // abcedfg -> gfdecba
        pHead = CreateLinkedList("abcedfg");
        pHead = ReverseLinkedList2(pHead);
        ASSERT_EQ(ConvertLinkedList(pHead),"gfdecba");
        ReleaseLinkedList(pHead);
    }
  • 相关阅读:
    Feb292012 个人核心竞争力的构建
    让读书成为一种习惯
    软件工厂方法(二):软件工厂应用
    Scrum之 站立例会
    信息系统开发平台OpenExpressApp - AutoUI自动生成界面
    信息系统开发平台OpenExpressApp-内置支持的属性编辑方式
    信息系统开发平台OpenExpressApp - 订单示例(Getting Started)
    需求入门: 原型开发
    信息系统开发平台OpenExpressApp - 学习必备知识
    从IT方法论来谈RUP
  • 原文地址:https://www.cnblogs.com/Quincy/p/4867221.html
Copyright © 2011-2022 走看看