Skip to content

Instantly share code, notes, and snippets.

class Solution {
public:
int evalRPN(vector<string> &tokens) {
int size = tokens.size();
stack<int> myStack;
for(int i = 0; i < size; i++) {
class Solution {
public:
void reverseWords(string &s) {
int len = s.length();
if(len == 0) return ;
int i = len - 1;
string words;
class Solution {
public:
ListNode *sortList(ListNode *head) {
if(head == NULL || head->next == NULL) return head;
mergeSort(head);
return head;
}
void split(ListNode * head, ListNode *& left, ListNode *& right) {
class Solution {
public:
void insertNode(ListNode*& head, ListNode* current) {
if(current == NULL) return;
if(head == NULL) {
head = current;
return;
}
class Node {
public:
Node* prev;
Node* next;
int key;
int val;
Node(Node* prev, Node* next, int key, int val): prev(prev),next(next), key(key), val(val) {};
Node(int k, int val): prev(NULL), next(NULL), key(k), val(val) {};
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> results;
if(!root) return results;
stack<TreeNode *> myStack;
stack<TreeNode *> output;
TreeNode * current = root;
myStack.push(current);
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> results;
stack<TreeNode*> myStack;
if(root == NULL) return results;
TreeNode * current = root;
class Solution {
public:
void reverse(ListNode *& head) {
if(head == NULL || head->next == NULL) return;
ListNode * pre = NULL;
ListNode * current = head;
ListNode * next = head;
while(current) {
next = current->next;
class Solution {
public:
bool hasCycle(ListNode * head, ListNode *& slow, ListNode *& fast) {
if(head == NULL) return false;
while(fast && fast->next) { // two conditions, bug!
slow = slow->next;
fast = fast->next->next;
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL) return false;
ListNode * slow = head;
ListNode * fast = head;
while(fast && fast->next ) {