牛客题霸NC78 c++版答案

题目链接

/*
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;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_43627017/article/details/109771043
今日推荐