leetcode-206.反转链表
文章目录
- leetcode-206.反转链表
 - 一.题目描述
 - 二.代码提交
 - 三.易错点
 
一.题目描述

二.代码提交

代码
class Solution {
  public:
    ListNode *reverseList(ListNode *head) {
        ListNode *temp; // 保存cur的下一个节点
        ListNode *cur = head;
        ListNode *pre = nullptr;
        while (cur != nullptr) {
            temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next
            cur->next = pre;  // 翻转操作
            // 更新pre 和 cur指针
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};
 
三.易错点




















