牛客题霸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;
}
};