当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- OPTIMIZER_ Trace details
- Python crawler actual combat details: crawling home of pictures
- Calculation script for time series data
- 直播预告 | 微服务架构学习系列直播第三期
- After brushing leetcode's linked list topic, I found a secret!
- PHPSHE 短信插件说明
- Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
- [performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
- 业内首发车道级导航背后——详解高精定位技术演进与场景应用
- 中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
猜你喜欢

事半功倍:在没有机柜的情况下实现自动化

從小公司進入大廠,我都做對了哪些事?

DevOps是什么

业内首发车道级导航背后——详解高精定位技术演进与场景应用

Examples of unconventional aggregation

How to demote a domain controller in Windows Server 2012 and later

Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的

Troubleshooting and summary of JVM Metaspace memory overflow

Flink的DataSource三部曲之二:内置connector

hadoop 命令总结
随机推荐
In depth understanding of the construction of Intelligent Recommendation System
Don't go! Here is a note: picture and text to explain AQS, let's have a look at the source code of AQS (long text)
IPFS/Filecoin合法性:保护个人隐私不被泄露
Basic principle and application of iptables
xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
快快使用ModelArts,零基础小白也能玩转AI!
[event center azure event hub] interpretation of error information found in event hub logs
Programmer introspection checklist
阿里云Q2营收破纪录背后,云的打开方式正在重塑
Python + appium automatic operation wechat is enough
TRON智能钱包PHP开发包【零TRX归集】
PHPSHE 短信插件说明
EOS创始人BM: UE,UBI,URI有什么区别?
C language 100 question set 004 - statistics of the number of people of all ages
Microservices: how to solve the problem of link tracing
Jmeter——ForEach Controller&Loop Controller
直播预告 | 微服务架构学习系列直播第三期
Network programming NiO: Bio and NiO
中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
htmlcss