链表LeetCode总结
创始人
2024-06-02 15:18:04
0
  • LeetCode题目
    • 203.移除链表元素
    • 206.反转链表
    • 160.相交链表
    • 707.设计链表
    • 24.两两交换链表中的节点
    • 19.删除链表的倒数第 N 个结点
    • 142.环形链表 II
  • 链表题目需要注意的点
    • ListNode结构体要会背
    struct ListNode {int val;ListNode* next;ListNode(int _val = 0, ListNode* _next = nullptr): val(_val), next(_next){}
    };
    // 结尾记得加分号
    
    • 先判断head节点是否为nullptr
    • 先判断有效性,再取next
    • 善用dummy哨兵节点和pre
    • 使用pre,cur,next,next2指代已有节点,使用temp指代新建节点
    • 前向迭代模板
    ListNode dummy = ListNode(-1, head);
    ListNode *cur = dummy, *next;
    for (next = cur->next; cur && next; next = cur->next) {...cur = cur->next;
    }
    
  • 链表题目常用方法与概念
    • 快慢指针,记得先判断快指针有效性,再取next
    • 相向指针
    • 相交链表,尾部对齐,再逐个检查尾部指针,相同处为相交处
    • 设计链表时,不要单独实现头插和尾插,统一用指定下标插入实现
    • 循环链表,两步指针与单步指针重合处,必定为head
    • 有环链表,在快慢指针重合处、head,同时出发,重合点为环的起点
  • 题目讲解:以综合题707和易错题24、142为例
    • 707.设计链表
    class MyLinkedList {struct ListNode {int val;ListNode* next;ListNode(int _val = 0, ListNode* _next = nullptr): val(_val), next(_next){}};private:ListNode* dummy;int size;public:MyLinkedList(): dummy(new ListNode()), size(0){}int get(int index){if (index >= size || index < 0)return -1;ListNode *cur = dummy, *next;int cur_index = 0;for (next = cur->next; cur && next; next = cur->next) {if (cur_index == index) {return next->val;}cur = cur->next;++cur_index;}return cur->val;}// 不要单独实现头插和尾插,统一用指定下标插入实现void addAtHead(int val) { addAtIndex(0, val); }void addAtTail(int val) { addAtIndex(size, val); }void addAtIndex(int index, int val){if (index > size)return;if (index < 0)index = 0;ListNode *cur = dummy, *next, *temp;int cur_index = 0;for (next = cur->next; cur; next = cur->next) {if (cur_index == index) {temp = new ListNode(val);temp->next = next;cur->next = temp;++size;break;}cur = cur->next;++cur_index;}}void deleteAtIndex(int index){if (index >= size)return;ListNode *cur = dummy, *next, *temp;int cur_index = 0;for (next = cur->next; cur && next; next = cur->next) {if (cur_index == index) {temp = next;cur->next = next->next;delete temp;--size;break;}cur = cur->next;++cur_index;}}
    };
    
    • 24.两两交换链表中的节点
    class Solution {
    public:ListNode* swapPairs(ListNode* head){if (!head || !(head->next))return head;ListNode dummy = ListNode(-1, head);ListNode *pre = &dummy, *cur = dummy.next, *next, *next2;for (next = cur->next; cur && next; next = cur->next) {next2 = next->next;pre->next = next;cur->next = next2;next->next = cur;// 易错点:pre要先更新,并且pre=curpre = cur;cur = next2;// 因为cur=next2,跳过了next// 所以更新后的cur无效,则结束,否则next的有效性无法确保if (!cur)break;}return dummy.next;}
    };
    
    • 142.环形链表 II
    class Solution {
    public:ListNode* detectCycle(ListNode* head){if (!head)return head;if (!(head->next))return head->next;ListNode *slow, *fast;// 易错点:快慢指针有效性判断for (slow = fast = head;slow->next && fast->next && fast->next->next;) {slow = slow->next;fast = fast->next->next;if (slow == fast) {for (; slow != head;) {slow = slow->next;head = head->next;}return head;}}return nullptr;}
    };
    

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...