牛客题霸NC78 c++版答案

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=188&&tqId=36164&rp=1&ru=/ta/job-code-high-week&qru=/ta/job-code-high-week/question-ranking

题目链接

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode* nHead = nullptr;
        while (pHead != nullptr) //每次在新链表的首部插入原链表的首结点
        {
            ListNode* temp = nHead;
            nHead = pHead;
            pHead = pHead->next;
            nHead->next = temp;
        }

        return nHead;
    }
};
全部评论

相关推荐

缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
评论
7
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务