链表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;}
    };
    

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...