• 《剑指offer》第二十四题:反转链表


    // 面试题24:反转链表
    // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的
    // 头结点。
    
    #include <cstdio>
    #include "List.h"
    
    ListNode* ReverseList(ListNode* pHead)
    {
        ListNode* pRevHead = nullptr;
        ListNode* pNode = pHead; //当前节点
        ListNode* pPrev = nullptr; //前一个节点
    
        while (pNode != nullptr)
        {
            ListNode* pNext = pNode->m_pNext;
    
            //如果到了尾节点
            if (pNext == nullptr)
                pRevHead = pNode;
    
            pNode->m_pNext = pPrev; //当前节点连接到上一个节点
    
            pPrev = pNode;
            pNode = pNext;
        }
        return pRevHead;
    }
    // ====================测试代码====================
    ListNode* Test(ListNode* pHead)
    {
        printf("The original list is: 
    ");
        PrintList(pHead);
    
        ListNode* pReversedHead = ReverseList(pHead);
    
        printf("The reversed list is: 
    ");
        PrintList(pReversedHead);
    
        return pReversedHead;
    }
    
    // 输入的链表有多个结点
    void Test1()
    {
        ListNode* pNode1 = CreateListNode(1);
        ListNode* pNode2 = CreateListNode(2);
        ListNode* pNode3 = CreateListNode(3);
        ListNode* pNode4 = CreateListNode(4);
        ListNode* pNode5 = CreateListNode(5);
    
        ConnectListNodes(pNode1, pNode2);
        ConnectListNodes(pNode2, pNode3);
        ConnectListNodes(pNode3, pNode4);
        ConnectListNodes(pNode4, pNode5);
    
        ListNode* pReversedHead = Test(pNode1);
    
        DestroyList(pReversedHead);
    }
    
    // 输入的链表只有一个结点
    void Test2()
    {
        ListNode* pNode1 = CreateListNode(1);
    
        ListNode* pReversedHead = Test(pNode1);
    
        DestroyList(pReversedHead);
    }
    
    // 输入空链表
    void Test3()
    {
        Test(nullptr);
    }
    
    int main(int argc, char* argv[])
    {
        Test1();
        Test2();
        Test3();
    
        return 0;
    }
    测试代码

    分析:多考虑边界值。

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
                val(x), next(NULL) {
        }
    };*/
    class Solution {
    public:
        ListNode* ReverseList(ListNode* pHead) {
            
            ListNode* pRevHead = nullptr;
            ListNode* pNode = pHead;
            ListNode* pPrev = nullptr;
            
            while (pNode != nullptr)
            {
                ListNode* pNext = pNode->next;
                
                if (pNext == nullptr)
                    pRevHead = pNode;
                
                pNode->next = pPrev;
                
                pPrev = pNode;
                pNode = pNext;
            }
            return pRevHead;
        }
    };
    牛客网提交代码

      

    /*
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) :
                val(x), next(NULL) {
        }
    };*/
    class Solution {
    public:
        ListNode* ReverseList(ListNode* pNode) {
            
            //1.链表为空 2.只有一个元素 3.递归到链表尾
            if(pNode == nullptr || pNode->next == nullptr)
                return pNode;
             
            //先反转后面的链表,寻找末尾节点
            ListNode* pReverseNode = ReverseList(pNode->next);
             
            //再将当前节点设置为下一节点的后续节点
            pNode->next->next = pNode;
            pNode->next = nullptr;
             
            return pReverseNode;
        }
    };
    递归实现反转链表
  • 相关阅读:
    Javascript中的Math.max()和Math.min()
    附件预览项目采坑记
    移动设备后台的理解
    网络是怎么连接的?(进阶一)
    Git建立本地分支和远程分支的映射关系
    springboot使用redis实现发布与订阅
    centos下安装配置mongodb
    vue循环时设置多选框禁用状态,v-for
    谷歌浏览器postman插件安装,亲测可用
    element-ui 无法对绑定表单的对象中的对象属性进行验证
  • 原文地址:https://www.cnblogs.com/ZSY-blog/p/12578490.html
Copyright © 2020-2023  润新知