struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
//核心思路:将前后两个节点的指向关系交换从->变成<-
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* pre = nullptr;
ListNode* cur = pHead;
ListNode* next = nullptr;
while(cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};
C++ 反转单向链表
最新推荐文章于 2023-04-19 22:53:17 发布