> 文章列表 > 看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21

目录

    • 前言
    • 算法题(LeetCode刷题203移除链表元素)—(保姆级别讲解)
    • 算法题(LeetCode刷题707.设计链表)—(保姆级别讲解)
      • 代码参考:
    • 算法题(LeetCode刷题206.反转链表)—(保姆级别讲解)
      • 算法思想(重要):
      • 双指针法代码:
      • 递归法(从前往后翻转指针)代码:
      • 递归法(从后往前翻转指针)代码:
    • 结束语

前言

本文章一部分内容参考于《代码随想录》----如有侵权请联系作者删除即可,撰写本文章主要目的在于记录自己学习体会并分享给大家,全篇并不仅仅是复制粘贴,更多的是加入了自己的思考,希望读完此篇文章能真正帮助到您!!!

算法题(LeetCode刷题203移除链表元素)—(保姆级别讲解)

力扣题目链接

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21
关于这个算法思想,我在之前的文章中已经提过,在这里不再赘述。

链表删除、清空和销毁-----2021-08-30

算法题(LeetCode刷题707.设计链表)—(保姆级别讲解)

力扣题目链接

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21
关于这个算法思想,我在之前的文章中已经提过,在这里不再赘述。

链表初始化、插入、遍历功能实现----2021-08-17

链表删除、清空和销毁-----2021-08-30

代码参考:

class MyLinkedList {
public:// 定义链表节点结构体struct LinkedNode {int val;LinkedNode* next;LinkedNode(int val):val(val), next(nullptr){}};// 初始化链表MyLinkedList() {_dummyHead = new LinkedNode(0); // 这里定义的头结点 是一个虚拟头结点,而不是真正的链表头结点_size = 0;}// 获取到第index个节点数值,如果index是非法数值直接返回-1, 注意index是从0开始的,第0个节点就是头结点int get(int index) {if (index > (_size - 1) || index < 0) {return -1;}LinkedNode* cur = _dummyHead->next;while(index--){ // 如果--index 就会陷入死循环cur = cur->next;}return cur->val;}// 在链表最前面插入一个节点,插入完成后,新插入的节点为链表的新的头结点void addAtHead(int val) {LinkedNode* newNode = new LinkedNode(val);newNode->next = _dummyHead->next;_dummyHead->next = newNode;_size++;}// 在链表最后面添加一个节点void addAtTail(int val) {LinkedNode* newNode = new LinkedNode(val);LinkedNode* cur = _dummyHead;while(cur->next != nullptr){cur = cur->next;}cur->next = newNode;_size++;}// 在第index个节点之前插入一个新节点,例如index为0,那么新插入的节点为链表的新头节点。// 如果index 等于链表的长度,则说明是新插入的节点为链表的尾结点// 如果index大于链表的长度,则返回空// 如果index小于0,则在头部插入节点void addAtIndex(int index, int val) {if(index > _size) return;if(index < 0) index = 0;        LinkedNode* newNode = new LinkedNode(val);LinkedNode* cur = _dummyHead;while(index--) {cur = cur->next;}newNode->next = cur->next;cur->next = newNode;_size++;}// 删除第index个节点,如果index 大于等于链表的长度,直接return,注意index是从0开始的void deleteAtIndex(int index) {if (index >= _size || index < 0) {return;}LinkedNode* cur = _dummyHead;while(index--) {cur = cur ->next;}LinkedNode* tmp = cur->next;cur->next = cur->next->next;delete tmp;_size--;}// 打印链表void printLinkedList() {LinkedNode* cur = _dummyHead;while (cur->next != nullptr) {cout << cur->next->val << " ";cur = cur->next;}cout << endl;}
private:int _size;LinkedNode* _dummyHead;};

算法题(LeetCode刷题206.反转链表)—(保姆级别讲解)

力扣题目链接

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21
看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21

算法思想(重要):

  1. 双指针法
  2. 递归法(从前往后翻转指针)
  3. 递归法(从后往前翻转指针)

双指针法代码:

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

为了更能让大家了解暴力解法的算法思想,作者特意画了一张图供大家观看!!!

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21
看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21

递归法(从前往后翻转指针)代码:

class Solution {
public:ListNode* reverse(ListNode* pre,ListNode* cur){if(cur == NULL) return pre;ListNode* temp = cur->next;cur->next = pre;// 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步// pre = cur;// cur = temp;return reverse(cur,temp);}ListNode* reverseList(ListNode* head) {// 和双指针法初始化是一样的逻辑// ListNode* cur = head;// ListNode* pre = NULL;return reverse(NULL, head);}};

//可以和上面的双指针法代码进行对比参考,代码实现原理一样,这里就不再赘述了。

递归法(从后往前翻转指针)代码:

class Solution {
public:ListNode* reverseList(ListNode* head) {// 边缘条件判断if(head == NULL) return NULL;if (head->next == NULL) return head;// 递归调用,翻转第二个节点开始往后的链表ListNode *last = reverseList(head->next);// 翻转头节点与第二个节点的指向head->next->next = head;// 此时的 head 节点为尾节点,next 需要指向 NULLhead->next = NULL;return last;}
}; 

为了更能让大家了解暴力解法的算法思想,作者特意画了一张图供大家观看!!!

看完这篇文章你就彻底懂啦{保姆级讲解}-----(LeetCode刷题203.707.206翻转链表) 2023.4.21

结束语

如果觉得这篇文章还不错的话,记得点赞 ,支持下!!!