当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
随机推荐
在大规模 Kubernetes 集群上实现高 SLO 的方法
怎么理解Python迭代器与生成器?
Using consult to realize service discovery: instance ID customization
Existence judgment in structured data
向北京集结!OpenI/O 2020启智开发者大会进入倒计时
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
Python + appium automatic operation wechat is enough
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
Vue 3 responsive Foundation
100元扫货阿里云是怎样的体验?
选择站群服务器的有哪些标准呢?
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
A debate on whether flv should support hevc
Dapr實現分散式有狀態服務的細節
How long does it take you to work out an object-oriented programming interview question from Ali school?
Sort the array in ascending order according to the frequency
Jmeter——ForEach Controller&Loop Controller
How do the general bottom buried points do?