当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
如何将数据变成资产?吸引数据科学家
事半功倍:在没有机柜的情况下实现自动化
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
PHP应用对接Justswap专用开发包【JustSwap.PHP】
数据产品不就是报表吗?大错特错!这分类里有大学问
至联云解析:IPFS/Filecoin挖矿为什么这么难?
Just now, I popularized two unique skills of login to Xuemei
中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
從小公司進入大廠,我都做對了哪些事?
Swagger 3.0 天天刷屏,真的香嗎?
随机推荐
加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?
Top 10 best big data analysis tools in 2020
Sort the array in ascending order according to the frequency
EOS创始人BM: UE,UBI,URI有什么区别?
Let the front-end siege division develop independently from the back-end: Mock.js
《Google軟體測試之道》 第一章google軟體測試介紹
Python自动化测试学习哪些知识?
htmlcss
Grouping operation aligned with specified datum
xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
Subordination judgment in structured data
網路程式設計NIO:BIO和NIO
DevOps是什么
Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
PHP应用对接Justswap专用开发包【JustSwap.PHP】
Character string and memory operation function in C language
It's so embarrassing, fans broke ten thousand, used for a year!
Filecoin主网上线以来Filecoin矿机扇区密封到底是什么意思