• [算法练习] 反转链表


    题目说明:

    输入一个链表的头结点,反转该链表,并返回反转后链表的头结点。链表结点定义如下:
    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);
    }
  • 相关阅读:
    工具类
    开发中用到的工具
    项目中另外添加有用的文件:404
    如何组织项目结构:约定优于配置
    媒体查询
    响应式网站开发需要掌握的技术及国内外主流浏览器
    响应式网站概念
    sql存储过程,raisError后要return错误代码,过程最后要return 0
    delphi用TAdoStoredProc调用存储过程,兼容sql2005、2008、2014的远程事务问题
    网页视频下载牛逼工具,支持各种格式转换,比如腾讯视频格式qlv转mp4
  • 原文地址:https://www.cnblogs.com/Quincy/p/4867221.html
Copyright © 2020-2023  润新知