当前位置:网站首页>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
边栏推荐
- Eureka自我保护
- ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
- ES6 从入门到精通 # 14:迭代器 Iterator 的用法
- arm-4-裸板开发
- Jpa 查询view or 无主键的table
- 断开和服务器共享连接的方法「建议收藏」
- The older tester has just passed the "hurdle" of being 35 years old, and I want to tell you something from my heart
- 领跑政务云,连续五年中国第一
- FreeRTOS任务基础
- deepstream学习笔记(三):deepstream-imagedata-multistream解析与接入适配yolov5模型测试
猜你喜欢
随机推荐
【SSL集训DAY3】控制棋盘【二分图匹配】
Digital wallets, red sea ecological rapid introduction of small programs can help capture device entry wisdom
【猜凶手,猜名次,杨辉三角】经典小学奥数的代码逻辑是什么?
Distributed database problem (3): data consistency
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
下载markdown软件Obsidian(解决官网下载速度慢)
直播平台怎么搭建,原生js实现编辑器撤消/恢复功能
E - Sugoroku 3(期望dp)
When knowledge and action are one
ECCV 2022 | 微软开源TinyViT :搞定小模型的预训练能力
防火墙之系统防护
YOLOV5学习笔记(七)——训练自己数据集
新开窗口 展示协议
基于 LSTM 的分布式能源发电预测(Matlab代码实现)
Wireshark经典实践和面试13点总结
HStreamDB v0.9 发布:分区模型扩展,支持与外部系统集成
WPF DataGrid 使用数据模板
MATLB|和她跌宕起伏最终到达人生之峰【浪漫旅途】
Wireshark classic practice and interview 13-point summary
【集训DAY3】阶乘【数学】









