week15
141 - 环形链表
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow = head, *fast = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
};142 - 环形链表II
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head, *fast = head;
bool flag = false;
// 1. 判断是否有环
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
flag = true;
break;
}
}
if(!flag) return nullptr;
// 2. 记录环的长度 并让cur比head提前走 环那么长的指针
// 这样他们再次相遇的时候一定就是起点
ListNode* cur = head;
do
{
slow = slow->next;
cur = cur->next;
}while(slow != fast);
// 3. cur 比 head 提前了一整个环的长度 同时走 直到相遇 就是起点
while(cur != head)
{
cur = cur->next;
head = head->next;
}
return head;
}
};143 - 重排链表
144 - 二叉树的前序遍历
145 - 二叉树的后序遍历
146 - LRU 缓存机制

147 - 对链表进行插入排序
148 - 排序链表
149 - 直线上最多的点数
150 - 逆波兰表达式求值
Last updated
