首页 > 其他 > 详细

翻转链表

时间:2020-09-15 17:02:05      阅读:52      评论:0      收藏:0      [点我收藏+]
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) 
    {
        if (pHead == nullptr || pHead -> next == nullptr)
            return pHead;
        ListNode* currentNode = pHead;
        ListNode* preNode = nullptr;
        while (currentNode -> next != nullptr)
        {
            ListNode* nextNode = currentNode -> next;
            currentNode -> next = preNode;
            preNode = currentNode;
            currentNode = nextNode;
        }
        currentNode -> next = preNode;
        return currentNode;
        
        
    }
};

 

翻转链表

原文:https://www.cnblogs.com/waterrzhang/p/13672992.html

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