当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Synchronous configuration from git to consult with git 2consul
- Every day we say we need to do performance optimization. What are we optimizing?
- Vuejs development specification
- Filecoin主网上线以来Filecoin矿机扇区密封到底是什么意思
- It's so embarrassing, fans broke ten thousand, used for a year!
- 嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
- Swagger 3.0 天天刷屏,真的香嗎?
- 条码生成软件如何隐藏部分条码文字
- Subordination judgment in structured data
- 比特币一度突破14000美元,即将面临美国大选考验
猜你喜欢

小程序入门到精通(二):了解小程序开发4个重要文件

连肝三个通宵,JVM77道高频面试题详细分析,就这?

DTU连接经常遇到的问题有哪些

Troubleshooting and summary of JVM Metaspace memory overflow

人工智能学什么课程?它将替代人类工作?

ES6学习笔记(五):轻松了解ES6的内置扩展对象

Can't be asked again! Reentrantlock source code, drawing a look together!

加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?

Not long after graduation, he earned 20000 yuan from private work!

How to select the evaluation index of classification model
随机推荐
有关PDF417条码码制的结构介绍
助力金融科技创新发展,ATFX走在行业最前列
Menu permission control configuration of hub plug-in for azure Devops extension
2019年的一个小目标,成为csdn的博客专家,纪念一下
[performance optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
Flink on paasta: yelp's new stream processing platform running on kubernetes
Analysis of react high order components
Elasticsearch 第六篇:聚合統計查詢
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
DRF JWT authentication module and self customization
人工智能学什么课程?它将替代人类工作?
Jmeter——ForEach Controller&Loop Controller
Cos start source code and creator
Why do private enterprises do party building? ——Special subject study of geek state holding Party branch
100元扫货阿里云是怎样的体验?
Working principle of gradient descent algorithm in machine learning
ES6 essence:
[event center azure event hub] interpretation of error information found in event hub logs
Real time data synchronization scheme based on Flink SQL CDC
DTU连接经常遇到的问题有哪些