class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head || !head->next) return 0;
ListNode* a = head, *b = head->next;
while(a && b)
{
if(a == b) return true;
a = a->next;
b = b->next;
if(b) b = b->next;
}
return false;
}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head) return head;
ListNode* a = head, *b = head;
while(a && b)
{
a = a->next;
b = b->next;
if(b) b = b->next;
else return 0;
if(a == b)
{
a = head;
while(a != b)
{
a = a->next;
b = b->next;
}
return b;
}
}
return 0;
}
};
class Solution {
public:
void reorderList(ListNode* head) {
if(!head) return;
int n = 0;
for(auto p = head; p; p = p->next) n ++;
if(n <= 2) return;
ListNode* mid = head;
for(int i = 0; i < (n + 1) / 2 - 1; i ++) mid = mid->next;
ListNode* a = mid->next;
mid->next = NULL;
ListNode* b = a->next;
a->next = NULL;
while(b)
{
ListNode* next = b->next;
b->next = a;
a = b;
b = next;
}
b = head;
while(a)
{
ListNode* next = a->next;
a->next = b->next;
b->next = a;
a = next;
b = b->next->next;
}
}
};
class Solution {
public:
vector<int> res;
vector<int> preorderTraversal(TreeNode* root) {
if(!root) return res;
dfs(root);
return res;
}
void dfs(TreeNode* u)
{
if(!u) return;
res.push_back(u->val);
if(u->left) dfs(u->left);
if(u->right) dfs(u->right);
}
};
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> stk;
vector<int> res;
TreeNode* p = root;
while(p || !stk.empty())
{
while(p)
{
res.push_back(p->val);
stk.push(p);
p = p->left;
}
p = stk.top()->right;
stk.pop();
}
return res;
}
};
class Solution {
public:
vector<int> res;
vector<int> postorderTraversal(TreeNode* root) {
if(!root) return res;
dfs(root);
return res;
}
void dfs(TreeNode* u)
{
if(u->left) dfs(u->left);
if(u->right) dfs(u->right);
res.push_back(u->val);
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> stk;
TreeNode* p = root, * lastVisited = NULL;
while(p || !stk.empty())
{
while(p)
{
stk.push(p);
p = p->left;
}
p =stk.top();
if(p->right == NULL || p->right == lastVisited)
{
stk.pop();
res.push_back(p->val);
lastVisited = p;
p = NULL;
}
else p = p->right;
}
return res;
}
};
class LRUCache {
public:
struct Node
{
int key, value;
Node(int k, int v) : key(k), value(v){}
};
int capacity;
list<Node> cacheList;
unordered_map<int, list<Node>::iterator>cacheMap;
LRUCache(int c) : capacity(c) {
}
int get(int key) {
if(cacheMap.find(key) == cacheMap.end())
return -1;
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
return cacheMap[key]->value;
}
void put(int key, int value) {
if(cacheMap.find(key) == cacheMap.end())
{
if(cacheList.size() == capacity)
{
cacheMap.erase(cacheList.back().key);
cacheList.pop_back();
}
cacheList.push_front(Node(key, value));
cacheMap[key] = cacheList.begin();
}
else
{
cacheMap[key]->value = value;
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
}
}
};
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode *dummy = new ListNode(-1);
while(head)
{
ListNode*p = dummy;
while(p->next && p->next->val <= head->val) p = p->next;
ListNode* next = head->next;
head->next = p->next;
p->next = head;
head = next;
}
return dummy->next;
}
};
class Solution {
public:
ListNode* sortList(ListNode* head) {
return head == NULL ? NULL : mergeSort(head);
}
ListNode* mergeSort(ListNode* node)
{
if(!node->next) return node;
ListNode* a = node, *b = node;
ListNode* breakN = node;
while(a && a->next)
{
a = a->next->next;
breakN = b;
b = b->next;
}
breakN->next = NULL;
ListNode*l = mergeSort(node);
ListNode*r = mergeSort(b);
return mergeTwoList(l, r);
}
ListNode* mergeTwoList(ListNode*l1, ListNode*l2)
{
ListNode*dummy = new ListNode(-1);
ListNode*cur = dummy;
while(l1 && l2)
{
if(l1->val <= l2->val)
{
cur->next = l1;
l1 = l1->next;
}
else
{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = (l1 == NULL ? l2 : l1);
return dummy->next;
}
};
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
if(points.empty()) return 0;
int res = 1;
for(int i = 0; i < points.size(); i ++)
{
int ver = 1, dup = 0;
unordered_map<long double, int> hash;
for(int j = i + 1; j < points.size(); j ++)
if(points[i][0] == points[j][0])
{
ver ++;
if(points[i][1] == points[j][1]) dup ++;
}
for(int j = i + 1; j < points.size(); j ++)
if(points[i][0] != points[j][0])
{
long double slope = (long double)(points[i][1] - points[j][1]) / (points[i][0] - points[j][0]);
if(!hash.count(slope)) hash[slope] = 2;
else hash[slope] ++;
res = max(res, hash[slope] + dup);
}
res = max(res, ver);
}
return res;
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int>stk;
for(auto &t : tokens)
if(t == "+" || t == "-" || t == "*" || t == "/")
{
int a = stk.top();
stk.pop();
int b = stk.top();
stk.pop();
if(t == "+") stk.push(a + b);
else if(t == "-") stk.push(b - a);
else if(t == "*") stk.push(a * b);
else stk.push(b / a);
}
else stk.push(atoi(t.c_str()));
return stk.top();
}
};