This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| int evalRPN(vector<string> &tokens) { | |
| int size = tokens.size(); | |
| stack<int> myStack; | |
| for(int i = 0; i < size; i++) { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| void reverseWords(string &s) { | |
| int len = s.length(); | |
| if(len == 0) return ; | |
| int i = len - 1; | |
| string words; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| void insertNode(ListNode*& head, ListNode* current) { | |
| if(current == NULL) return; | |
| if(head == NULL) { | |
| head = current; | |
| return; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) {}; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| vector<int> preorderTraversal(TreeNode *root) { | |
| vector<int> results; | |
| stack<TreeNode*> myStack; | |
| if(root == NULL) return results; | |
| TreeNode * current = root; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public: | |
| bool hasCycle(ListNode *head) { | |
| if(head == NULL) return false; | |
| ListNode * slow = head; | |
| ListNode * fast = head; | |
| while(fast && fast->next ) { |
OlderNewer