题型
反转整个链表
反转链表前N个节点
链表内指定区间反转
K个节点一组反转链表
反转整个链表
思路1:
迭代,定义三个辅助链表节点指针pre、cur、nxt,pre为遍历链表时所在节点cur的前驱节点,nxt为cur的后驱节点。
步骤:
1.nxt指向cur下一个节点;
2. cur的下一个节点修改为pre;
3. pre指向当前节点cur;
4. cur指向nxt;
代码:
/*
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* nxt=pHead;
while(cur){
nxt=cur->next;
cur->next =pre;