Algorithm in LeetCode —— Linked List

Tips for Linked Lists:

  • Cleverly introduce a dummy head node. This can make traversal and processing logic more uniform.
  • Use recursion flexibly. By defining the right recursive conditions, recursion can solve certain problems elegantly. However, note that some problems should not be solved recursively, because excessive recursion depth can lead to timeouts and stack overflows.
  • Reverse a range within a linked list. Problem 92.
  • Find the middle node of a linked list. Problem 876. Find the nth node from the end of a linked list. Problem 19. The answer can be obtained with just one traversal.
  • Merge K sorted linked lists. Problems 21 and 23.
  • Classify linked list nodes. Problems 86 and 328.
  • Sort a linked list with required time complexity O(n * log n) and space complexity O(1). There is only one approach: merge sort, using top-down merging. Problem 148.
  • Determine whether a linked list has a cycle; if it does, output the index of the intersection point of the cycle. Determine whether two linked lists intersect; if they do, output the intersection point. Problems 141, 142, and 160.
TitleSolutionDifficultyTimeSpaceFavorites
2. Add Two NumbersGoMediumO(n)O(1)
19. Remove Nth Node From End of ListGoMediumO(n)O(1)
21. Merge Two Sorted ListsGoEasyO(log n)O(1)
23. Merge k Sorted ListsGoHardO(log n)O(1)❤️
24. Swap Nodes in PairsGoMediumO(n)O(1)
25. Reverse Nodes in k-GroupGoHardO(log n)O(1)❤️
61. Rotate ListGoMediumO(n)O(1)
82. Remove Duplicates from Sorted List IIGoMediumO(n)O(1)
83. Remove Duplicates from Sorted ListGoEasyO(n)O(1)
86. Partition ListGoMediumO(n)O(1)❤️
92. Reverse Linked List IIGoMediumO(n)O(1)❤️
109. Convert Sorted List to Binary Search TreeGoMediumO(log n)O(n)
141. Linked List CycleGoEasyO(n)O(1)❤️
142. Linked List Cycle IIGoMediumO(n)O(1)❤️
143. Reorder ListGoMediumO(n)O(1)❤️
147. Insertion Sort ListGoMediumO(n)O(1)
148. Sort ListGoMediumO(log n)O(n)❤️
160. Intersection of Two Linked ListsGoEasyO(n)O(1)❤️
203. Remove Linked List ElementsGoEasyO(n)O(1)
206. Reverse Linked ListGoEasyO(n)O(1)
234. Palindrome Linked ListGoEasyO(n)O(1)
237. Delete Node in a Linked ListGoEasyO(n)O(1)
328. Odd Even Linked ListGoMediumO(n)O(1)
445. Add Two Numbers IIGoMediumO(n)O(n)
725. Split Linked List in PartsGoMediumO(n)O(1)
817. Linked List ComponentsGoMediumO(n)O(1)
707. Design Linked ListGoEasyO(n)O(1)
876. Middle of the Linked ListGoEasyO(n)O(1)❤️
1019. Next Greater Node In Linked ListGoMediumO(n)O(1)