当前位置:网站首页>LeetCode常见题型——链表
LeetCode常见题型——链表
2022-08-09 23:29:00 【贫道绝缘子】
1.算法思路
(单)链表是由节点和指针构成的数据结构,每个节点存有一个值,和一个指向下一个节点的指针,因此很多链表问题可以用递归来处理。
不同于数组,链表并不能直接获取任意节点的值,必须要通过指针找到该节点后才能获取其值。同理,在未遍历到链表结尾时,我们也无法知道链表的长度,除非依赖其他数据结构储存长度。
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
由于在进行链表操作时,尤其是删除节点时,经常会因为对当前节点进行操作而导致内存或指针出现问题。有两个小技巧可以解决这个问题:
- 尽量处理当前节点的下一个节点而非当前节点本身
- 建立一个虚拟节点(dummy node),使其指向当前链表的头节点,这样即使原链表所有节点全被删除,也会有一个dummy 存在,返回dummy->next 即可。
在刷LeetCode 的时候,如果想要删除一个节点,可以直接进行指针操作而无需回收内存。实际做软件工程时,对于无用的内存,尽量显式回收或利用智能指针。
2.常见题型
LeetCode-206. Reverse Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, reverse the list, and returnthe reversed list.https://blog.csdn.net/qq_15711195/article/details/126133049LeetCode-21. Merge Two Sorted Lists [C++][Java]_贫道绝缘子的博客-CSDN博客You are given the heads of two sorted linked listslist1andlist2. Merge the two lists in a onesortedlist. The list should be made by splicing together the nodes of the first two lists.
https://blog.csdn.net/qq_15711195/article/details/126170115LeetCode-24. Swap Nodes in Pairs [C++][Java]_贫道绝缘子的博客-CSDN博客Given alinked list, swap every two adjacent nodes and return its head. You must solve the problem withoutmodifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
https://blog.csdn.net/qq_15711195/article/details/126189263LeetCode-160. Intersection of Two Linked Lists [C++][Java]_贫道绝缘子的博客-CSDN博客Given the heads of two singly linked-listsheadAandheadB, returnthe node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull.
https://blog.csdn.net/qq_15711195/article/details/126190342LeetCode-234. Palindrome Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, returntrueif it is a palindrome.
https://blog.csdn.net/qq_15711195/article/details/126190827LeetCode-82. Remove Duplicates from Sorted List II [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a sorted linked list,delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Returnthe linked listsortedas well.
https://blog.csdn.net/qq_15711195/article/details/126191494?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126191494%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=5TT04LeetCode-83. Remove Duplicates from Sorted List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a sorted linked list,delete all duplicates such that each element appears only once. Returnthe linked listsortedas well.
https://blog.csdn.net/qq_15711195/article/details/122394297LeetCode-328. Odd Even Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and returnthe reordered list. Thefirstnode is consideredodd, and thesecondnode iseven, and so on.
https://blog.csdn.net/qq_15711195/article/details/126206743?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126206743%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=61ROWLeetCode-19. Remove Nth Node From End of List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a linked list, remove thenthnode from the end of the list and return its head.
https://blog.csdn.net/qq_15711195/article/details/126220472?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126220472%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=KYQhULeetCode-148. Sort List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a linked list, returnthe list after sorting it inascending order.
https://blog.csdn.net/qq_15711195/article/details/126220508?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126220508%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=Hxt2J
边栏推荐
- CAD 绘制圆角处理
- 【SSL集训DAY2】Sort【树状数组】
- 南大通用数据库-Gbase-8a-学习-04-部署分布式集群
- redis分布式锁代码示例
- 为什么不建议你在 Docker 中跑 Mysql ?
- Spark基础【RDD单Value类型转换算子】
- Dry goods!Towards robust test-time adaptation
- 下班后用微信处理工作时发病身亡,法院判决:工伤!
- 《动手学深度学习》(八) -- 多尺度标检测和单发多框检测
- Travel with Shengteng: See all the AI attractions in Jinling City in one day
猜你喜欢
数字孪生智慧制造生产线项目实施方案,平台认知与概念
go语言的并发原理(goroutine)
In-depth understanding of multithreading (Part 1)
dlopen failed: library "libtaml.so" not found
Description of AirFlow
GoLang 使用 goroutine 停止的几种办法
【Infiltration tool】Browser data export tool
YOLOV5 study notes (7) - training your own data set
JVM Memory and Garbage Collection - 10. Direct Memory
【集训DAY5】快速排序【模拟】【数学】
随机推荐
《动手学深度学习》(八) -- 多尺度标检测和单发多框检测
C language learning journey [operator (incomplete version)]
MQTT X Web:在线的 MQTT 5.0 客户端工具
YOLOV5 study notes (7) - training your own data set
下载markdown软件Obsidian(解决官网下载速度慢)
工程 (七) ——PolarSeg点云语义分割
ES6 从入门到精通 # 14:迭代器 Iterator 的用法
直播间搭建,按钮左滑出现删除等操作按钮
领跑政务云,连续五年中国第一
SRv6 performance measurement
什么是服务治理
基于ABP的AppUser对象扩展
MATLB|和她跌宕起伏最终到达人生之峰【浪漫旅途】
南大通用数据库-Gbase-8a-学习-04-部署分布式集群
今日睡眠质量记录61分
共创 Ray 中文社区,Ray Forward Meetup 2022 直播邀你参加!
经济衰退即将来临前CIO控制成本的七种方法
【C语言】通讯录《静态内存版本》
【集训DAY3】阶乘【数学】
《MySQL入门很轻松》第4章:数据表中存放的数据类型