<aside>
📚 Key Points:
- When dereferencing a ListNode, make sure it is not a NULL pointer.
- Never ever lose control of the head/newHead pointer of a Llinked List
- A ListNode head reference is the only way to represent the entire Linked List.
- Good Coding Practices:
- Better not to move head directly; create a cur pointer to traverse the list instead
- Coner case: head == null || head.next == null || k < 0
</aside>
易错点/Take-away Summary
- [ ] 每次过例子,准备一个全面的例子(cover all cases),把例子完整的过完,判断需要什么指针,指针怎么移动,有什么cases,终止条件是什么。不要过完前一两个,发现规律后,马上写代码。
- [ ] 改变箭头方向之后,需要断开的连接“一定要断开”。 例如,找中点题里,mid.next = null; dedup题里,slow.next = null
- [ ] output的头节点会改变? —> Use dummy node to secure the new head
- [ ] use dummy node to cover edge cases to make the code-writing easy and clean
- [ ] dummy node 一定要跟 head连接在一起
- [ ] 用dummy node方便的把list group到需要的小组,最后再拼接。group之后再拼接时,连接新的group link之后一定要把旧的link断开。例如,partition list 题,和 even odd 题
- [ ] When dereferencing a ListNode, make sure it is not a NULL pointer.
- [ ] A ListNode head reference is the only way to represent the entire Linked List.
- [ ] Better not to move head directly; create a cur pointer to traverse the list instead
- [ ] 每一轮的traversal,一定要移动所有的applicable指针。而且,顺序matters 顺序matters 顺序matters。错误的指针移动顺序可能导致丢失下一个节点的control
- [ ] 移动指针时,其中一个list变成null怎么办? 所有的corner case都要照顾到位 (corner case疯狂多的题: Add Two Numbers)
- [ ] 写 while loop 条件 或者 if conditional 的时候,一定到确保 condition里面的值不会有NPE。in while loop, must write”cur ≠ null” before writing “cur.value < target” and move cur pointer within the loop.
- [ ] Cycle exists in a Linked List == ListNode address can be seen twice durign traversal
- [ ] 两两比较完,post processing 别忘了 - 把剩下的/没比的node全部接到最后面。例如: merge two sorted array
- [ ] When i create a new/next pointer to store the next node of cur node and change the arrow direction, ensure I call the nextNode as “next not cur.next”
- [ ] NPE NPE NPE - The biggest issue from Linked List problem. For any if conditionals, make sure to “consider the null case first before considering any other non-null cases.”
- [ ] Make sure “not to lose control of head & newHead”. If needed, create a dummyNode to keep the code clear and clean.
- [ ] 每个step千万不要忘记移动指针
- [ ] 过例子的时候,想好要移动多少个步数,count nodes between the range to see how many steps I need to move. for(int i = 0; i < numOfNodesINeedToMove; i++) (i.e LeetCode 92)
- leftNode定位时,for loop 要移动 left - 1个node
- rightNode定位时,for loop 需要移动 right - left + 1个node
- [ ] 每次for loop完/reverse每个node之后,指针已经移动到下一轮要遍历的node上。所有出了for loop之后,要清楚的知道你的指针到底在哪里。
<aside>
🌵 Table of Contents
</aside>
Two Pointers
<aside>
📌 Two Pointers Problem Summary**:**
- output的头节点会改变? —> Use dummy node to secure the new head
- 每一轮的traversal,一定要移动所有的applicable指针。而且,顺序matters 顺序matters 顺序matters。错误的指针移动顺序可能导致丢失下一个节点的control
- 移动指针时,其中一个list变成null怎么办? 所有的corner case都要照顾到位(i.e. Add Two Numbers)
- 写 while loop 条件,或者 if conditional 的时候,一定到确保 condition里面的值不会有NPE。in while loop, must write”cur ≠ null” before writing “cur.value < target” and move cur pointer within the loop.
</aside>
Date: June 15, 2023
Problem 1: Middle Node Of Linked List
Date: June 16, 2023
Problem 1-1: Check If Linked List Has A Cycle
Date: June 19, 2023
Problem 1-2: Cycle Node In Linked List
Date: June 19, 2023
Problem 2: Add Two Numbers
Date: June 24, 2023