- 题目地址
- c++ 实现
// -*- coding: utf-8 -*-
// @ Date : 2022/5/20 13:14
// @ Author : RichardLau_Cx
// @ file : Richard.cpp
// @ IDE : Dev-C++
// @ Source : nowcoder
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (pHead == nullptr) {
return nullptr;
}
ListNode* pre = nullptr;
ListNode* next = nullptr;
while (pHead != nullptr) {
next = pHead->next;
pHead->next = pre;
pre = pHead;
pHead = next;
}
return pre;
}
};
- Note:关键在于清晰结点之间的指向,以及不可过程“断指”。