当前位置:网站首页>Summary of common algorithms of linked list
Summary of common algorithms of linked list
2020-11-06 01:18:00 【Clamhub's blog】
1、 Sum two single linked lists
445. Addition of two numbers II
Better understanding of double stack method .
1 |
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) { |
Construct two stacks from two linked lists , Use the first in, last out feature of the stack , Do two list flashbacks and do calculations , Pay attention to rounding .
2、 Single linked list non recursively flipped , Without the aid of other data structures
206. Reverse a linked list
The flip of single linked list is to flip the pointer of two adjacent nodes . Two extra pointers are needed to store the front and back nodes of the current node .
1 |
public static ListNode reverseList(ListNode head) { |
3、 Palindrome list ( use O(n) Time complexity and O(1) Spatial complexity )
234. Palindrome list
Get the middle node position of the linked list through the fast and slow pointer , Reverse the second half of the list , After iteration, the linked list before and after , Whether the judgment value is the same .
1 |
public static boolean isPalindrome(ListNode head) { |
4、 Circular list
141. Circular list
Check whether there are rings in the linked list , There are two ways : Use HashSet Or speed pointer .
1 |
public static boolean hasCycle(ListNode head) { |
5、 Delete all the values in the linked list as val The node of
203. Remove linked list elements
A sentinel node is used to remove the linked list , Simplify the deletion of .
1 |
public static ListNode removeElements(ListNode head, int val) { |
6、 Merge two ordered lists
21. Merge two ordered lists
Need to use sentinel node .
1 |
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { |
summary
The commonly used algorithm of linked list often uses the fast and slow pointer and sentry node . Just use the sentinel node head There will be pre The nodes match .
版权声明
本文为[Clamhub's blog]所创,转载请带上原文链接,感谢
边栏推荐
- 一时技痒,撸了个动态线程池,源码放Github了
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Computer TCP / IP interview 10 even asked, how many can you withstand?
- Listening to silent words: hand in hand teaching you sign language recognition with modelarts
- Calculation script for time series data
- 直播预告 | 微服务架构学习系列直播第三期
- 幽默:黑客式编程其实类似机器学习!
- Process analysis of Python authentication mechanism based on JWT
- Chainlink将美国选举结果带入区块链 - Everipedia
- 数字城市响应相关国家政策大力发展数字孪生平台的建设
猜你喜欢
“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询
中小微企业选择共享办公室怎么样?
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
连肝三个通宵,JVM77道高频面试题详细分析,就这?
Troubleshooting and summary of JVM Metaspace memory overflow
PLC模拟量输入和数字量输入是什么
PHPSHE 短信插件说明
做外包真的很难,身为外包的我也无奈叹息。
Subordination judgment in structured data
随机推荐
《Google軟體測試之道》 第一章google軟體測試介紹
Chainlink将美国选举结果带入区块链 - Everipedia
Installing the consult cluster
直播预告 | 微服务架构学习系列直播第三期
全球疫情加速互联网企业转型,区块链会是解药吗?
PN8162 20W PD快充芯片,PD快充充电器方案
Polkadot series (2) -- detailed explanation of mixed consensus
In depth understanding of the construction of Intelligent Recommendation System
The practice of the architecture of Internet public opinion system
中小微企业选择共享办公室怎么样?
合约交易系统开发|智能合约交易平台搭建
Vue 3 responsive Foundation
【效能優化】納尼?記憶體又溢位了?!是時候總結一波了!!
制造和新的自动化技术是什么?
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
DTU连接经常遇到的问题有哪些
Leetcode's ransom letter
条码生成软件如何隐藏部分条码文字
Group count - word length
人工智能学什么课程?它将替代人类工作?