当前位置:网站首页>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
边栏推荐
- A Shanghai technology company was fined 220,000 for brushing orders, exposing the gray industry chain of online brushing
- C language learning journey [operator (incomplete version)]
- [C language] In-depth understanding of pointers and arrays (issue 4)
- 781. 森林中的兔子
- 安全知识培训——消防安全
- AppUser object extension based on ABP
- Eureka自我保护
- 分布式数据库难题(三):数据一致性
- 【Infiltration tool】Browser data export tool
- 漫谈缺陷管理的自动化实践方案
猜你喜欢
781. 森林中的兔子
Dry goods!Towards robust test-time adaptation
【集训DAY5】堆箱子【数学】
Today's sleep quality record 61 points
dlopen failed: library “libtaml.so“ not found
防火墙之系统防护
Wireshark classic practice and interview 13-point summary
服务发现@EnableDiscoveryClient
巴比特 | 元宇宙每日必读:国内首个数字人产业专项支持政策发布,2025年北京数字人产业规模将破500亿元...
恭喜获奖得主 | 互动有礼获赠 Navicat Premium
随机推荐
Spark基础【RDD单Value类型转换算子】
CAD 连接两个相交线
经济衰退即将来临前CIO控制成本的七种方法
CMake使用记录
CST Studio Suite 2021软件安装包和安装教程
conda新建环境时报错NotWritableError: The current user does not have write permissions
深入理解Aarch64内存管理
恭喜获奖得主 | 互动有礼获赠 Navicat Premium
阿雷的血压有些低
hql语言
领跑政务云,连续五年中国第一
MATLB|和她跌宕起伏最终到达人生之峰【浪漫旅途】
防火墙之系统防护
ES6 从入门到精通 # 13:数组的扩展方法二
数据库的备份与恢复「建议收藏」
Creo5.0 introductory tutorial free material
Creo5.0入门教程赠素材
Service Discovery @EnableDiscoveryClient
Today's sleep quality record 61 points
WPF DataGrid using data templates