Linked List与Sorting Algorithm结合
<aside> 💡 tips:能不用“global” variable 就不用 “global” variable
java没有真正的“global” variable,这里是指field.
Count the length of LinkedList
find the index - k element of it
Merge two sorted LinkedList
Queue and Stack
ListNode cur = head;
int count = 0;
for each step: check if cur is not null, count+1, cur move to next ListNode
TC: O(n), SC: O(1)
终止条件:index == k || cur == null
循环条件:index != k && cur != null
TC: O(min(n, k)), SC: O(1)
Never lose the control of head node, use pointer instead.
如果我们需要访问 p.next.next,必须要保证p != null && p.next != null
快慢指针法:slow走一步,fast走两步
终止条件:奇偶有不同
cases and stop condition:
TC: O(n) SC: O(1)
创建dummy node
<aside> 📌 SUMMARY:
</aside>
<aside> 📌 SUMMARY: LinkedList Reorder, MergeSort一定要记得断开中点和后一个元素。partition记得要curSmall.next = large; curLarge.next = null;
</aside>