当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 2019年的一个小目标,成为csdn的博客专家,纪念一下
- Flink on paasta: yelp's new stream processing platform running on kubernetes
- 深度揭祕垃圾回收底層,這次讓你徹底弄懂她
- Chainlink将美国选举结果带入区块链 - Everipedia
- Asp.Net Core learning notes: Introduction
- Sort the array in ascending order according to the frequency
- High availability cluster deployment of jumpserver: (6) deployment of SSH agent module Koko and implementation of system service management
- Skywalking series blog 1 - install stand-alone skywalking
- hadoop 命令总结
- Listening to silent words: hand in hand teaching you sign language recognition with modelarts
猜你喜欢
Subordination judgment in structured data
Network programming NiO: Bio and NiO
快快使用ModelArts,零基础小白也能玩转AI!
大数据应用的重要性体现在方方面面
一时技痒,撸了个动态线程池,源码放Github了
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
怎么理解Python迭代器与生成器?
Swagger 3.0 天天刷屏,真的香嗎?
阿里云Q2营收破纪录背后,云的打开方式正在重塑
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
随机推荐
(2)ASP.NET Core3.1 Ocelot路由
嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
03_ Detailed explanation and test of installation and configuration of Ubuntu Samba
OPTIMIZER_ Trace details
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
Chainlink将美国选举结果带入区块链 - Everipedia
至联云解析:IPFS/Filecoin挖矿为什么这么难?
Flink的DataSource三部曲之二:内置connector
阿里云Q2营收破纪录背后,云的打开方式正在重塑
助力金融科技创新发展,ATFX走在行业最前列
Nodejs crawler captures ancient books and records, a total of 16000 pages, experience summary and project sharing
速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)
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)
Why do private enterprises do party building? ——Special subject study of geek state holding Party branch
PN8162 20W PD快充芯片,PD快充充电器方案
人工智能学什么课程?它将替代人类工作?
This article will introduce you to jest unit test
PHP应用对接Justswap专用开发包【JustSwap.PHP】
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
Just now, I popularized two unique skills of login to Xuemei