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

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...