{"id":920,"date":"2024-10-15T21:55:16","date_gmt":"2024-10-15T13:55:16","guid":{"rendered":"http:\/\/114.55.108.251\/?p=920"},"modified":"2024-10-24T16:51:42","modified_gmt":"2024-10-24T08:51:42","slug":"leetcode-hot100%e9%93%be%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/guapicoding.com\/?p=920","title":{"rendered":"LeetCode hot100@\u94fe\u8868"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u5355\u94fe\u8868\u5b9a\u4e49\u65b9\u5f0f\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct ListNode {\n    int val;  \/\/ \u8282\u70b9\u4e0a\u5b58\u50a8\u7684\u5143\u7d20\n    ListNode *next;  \/\/ \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\n    ListNode(int x) : val(x), next(NULL) {}  \/\/ \u8282\u70b9\u7684\u6784\u9020\u51fd\u6570\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>STL\u94fe\u8868\uff1a<\/strong><br><code>list&lt;int> mylist;<\/code>  \u662f\u4e00\u4e2a\u53cc\u5411\u5faa\u73af\u94fe\u8868\uff0c\u4e0d\u9700\u8981\u5173\u5fc3\u8282\u70b9\u7ba1\u7406\u548c\u5185\u5b58\u7ba1\u7406(\u8be6\u7ec6\u8bed\u6cd5\u89c1c++\u63d0\u9ad8\u7f16\u7a0b)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/intersection-of-two-linked-lists\/\">160. \u76f8\u4ea4\u94fe\u8868<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>gap<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode(int x) : val(x), next(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {\n        ListNode *curA =  headA;\n        ListNode *curB =  headB;\n        int lenA = 1;\n        int lenB = 1;\n        while(curA-&gt;next != NULL) {\n            curA = curA-&gt;next;\n            lenA++;\n        }\n        while(curB-&gt;next != NULL) {\n            curB = curB-&gt;next;\n            lenB++;\n        }\n        \/\/ cout &lt;&lt; \"lenA = \" &lt;&lt; lenA &lt;&lt; endl;\n        \/\/ cout &lt;&lt; \"lenB = \" &lt;&lt; lenB &lt;&lt; endl;\n        \n        curA = headA;\n        curB = headB;\n        if(lenA &gt; lenB) {\n            swap(curA, curB);\n            swap(lenA, lenB);\n        }\n\n        int gap = lenB - lenA;\n        for(int i = 0; i &lt; gap; i++) curB = curB-&gt;next;\n        \/\/ cout &lt;&lt; \"val = \" &lt;&lt; (*curB).val &lt;&lt; endl;\n        while(curB != NULL) {\n            if(curB == curA) return curA;\n            curA = curA-&gt;next;\n            curB = curB-&gt;next;\n        }\n\n        return NULL;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/reverse-linked-list\/\">206. \u53cd\u8f6c\u94fe\u8868<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>\u53cc\u6307\u9488\u6cd5<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* reverseList(ListNode* head) {\n        ListNode *cur = head;\n        ListNode *pre = nullptr;\n        ListNode *tmp = nullptr;\n\n        while(cur) {\n            \/\/\u5148\u7528tmp\u4fdd\u5b58\u4e00\u4e0b\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u56e0\u4e3a\u4e00\u4f1a\u65b9\u5411\u6539\u53d8\u4e86\uff0c\u5c31\u627e\u4e0d\u5230\u5b83\u4e86\n            tmp = cur-&gt;next; \n\n            cur-&gt;next = pre; \/\/\u6539\u53d8\u65b9\u5411\n            pre = cur; \/\/pre\u5f80\u540e\u79fb\u4e00\u6b65\n            cur = tmp; \/\/\u628a\u4e4b\u524d\u4fdd\u5b58\u7684\u540e\u4e00\u4e2a\u8282\u70b9\u8fd8\u7ed9cur\n        }\n\n        return pre; \/\/\u6b64\u65f6cur\u5df2\u7ecf\u51fa\u754c\u4e86\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/palindrome-linked-list\/\">234. \u56de\u6587\u94fe\u8868<\/a>\u2705<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u628aval\u7528\u6570\u7ec4\u5b58\u8d77\u6765\u518d\u5224\u65ad\u5c31\u7b80\u5355\u4e86\uff0c\u56e0\u4e3a\u53ef\u4ee5\u53cc\u5411\u904d\u5386<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\npublic:\n    bool isPalindrome(ListNode* head) {\n        ListNode *cur = head;\n        vector&lt;int&gt; v;\n        while(cur) {\n            v.push_back(cur-&gt;val);\n            cur = cur-&gt;next;\n        }\n\n        int left = 0, right = v.size() - 1;\n        while(left &lt; right) {\n            if(v&#91;left++] != v&#91;right--]) return false;\n        }\n\n        return true;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/linked-list-cycle\/\">141. \u73af\u5f62\u94fe\u8868<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Solution1\uff1a\u7528\u54c8\u5e0c\u5b58\u8282\u70b9\u5730\u5740\u662f\u5426\u88ab\u8bbf\u95ee\u8fc7<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>Solution2\uff1a\u5982\u679c\u4e00\u4e2a\u94fe\u8868\u5b58\u5728\u73af\uff0c\u90a3\u4e48\u5feb\u6162\u6307\u9488\u5fc5\u7136\u4f1a\u76f8\u9047(\u7b2c\u4e8c\u6b21\u76f8\u9047\u65f6\u5feb\u6162\u6307\u9488\u79fb\u52a8\u6b21\u6570\u5dee\u503c\u5373\u4e3a\u73af\u7684\u957f\u5ea6)<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution1 {\npublic:\n    bool hasCycle(ListNode *head) {\n        unordered_map&lt;ListNode*, int&gt; mp;\n        ListNode *cur = head;\n        while(cur) {\n            if(mp&#91;cur]) {\n                return true;\n            }\n            mp&#91;cur]++;\n            cur = cur-&gt;next;\n        }\n\n        return false;\n    }\n};<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution2 {\npublic:\n    bool hasCycle(ListNode *head) {\n        ListNode *slow = head;\n        ListNode *fast = head;\n        while(fast) {\n            fast = fast-&gt;next;\n            if(fast) {\n                fast = fast-&gt;next;\n            }\n            \/\/\u5148\u5224\u65ad\u518d\u79fb\u52a8\u6162\u6307\u9488\uff0c\u5426\u5219\u4f1a\u6f0f\u6389\u53ea\u6709\u4e00\u4e2a\u8282\u70b9\u7684\u60c5\u51b5\n            if(fast == slow) return true; \n            slow = slow-&gt;next;  \n        }\n\n        return false;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/linked-list-cycle-ii\/\">142. \u73af\u5f62\u94fe\u8868 II<\/a>\u2705<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u7528\u54c8\u5e0c\u5b58\u8282\u70b9\u5730\u5740\u662f\u5426\u88ab\u8bbf\u95ee\u8fc7<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode(int x) : val(x), next(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode *detectCycle(ListNode *head) {\n        unordered_map&lt;ListNode*, int&gt; mp;\n        ListNode *cur = head;\n        while(cur) {\n            if(mp&#91;cur]) {\n                return cur;\n            }\n            mp&#91;cur]++;\n            cur = cur-&gt;next;\n        }\n\n        return NULL;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/merge-two-sorted-lists\/\">21. \u5408\u5e76\u4e24\u4e2a\u6709\u5e8f\u94fe\u8868<\/a>\u2705<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u4f18\u79c0\u7684\u94fe\u8868\u8bed\u6cd5\u7ec3\u4e60\u9898<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {\n        ListNode *merge = new ListNode(); \/\/\u5148\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u7ed3\u70b9\u4f5c\u4e3a\u65b0\u94fe\u8868\u5934\u8282\u70b9\u7684\u524d\u4e00\u4e2a\u8282\u70b9\n        ListNode *cur = merge; \/\/\u8ddf\u8e2a\u65b0\u94fe\u8868\u5c3e\u90e8\n        ListNode *cur1 = list1;\n        ListNode *cur2 = list2;\n  \n        while(cur1 != nullptr &amp;&amp; cur2 != nullptr) {\n            if(cur1-&gt;val &lt; cur2-&gt;val) {\n                cur-&gt;next = cur1;\n                cur = cur-&gt;next;\n                cur1 = cur1-&gt;next;\n            } else {\n                cur-&gt;next = cur2;\n                cur = cur-&gt;next;\n                cur2 = cur2-&gt;next;\n            }\n        }\n\n        if(cur1 == nullptr) cur-&gt;next = cur2;    \n        else if(cur2 == nullptr) cur-&gt;next = cur1;    \n\n        return merge-&gt;next;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/add-two-numbers\/\">2. \u4e24\u6570\u76f8\u52a0<\/a>\u2705<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u6a21\u62df\u76f8\u52a0\uff0c\u65e0\u9700\u8f6c\u6362\uff0c\u6700\u540e\u7edf\u4e00\u5904\u7406\u8fdb\u4f4d\u66f4\u7b80\u5355<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\n        ListNode *res = new ListNode();\n        ListNode *cur = res;\n        ListNode *cur1 = l1;\n        ListNode *cur2 = l2;\n        bool carry_bit = false; \n\n        while(cur1 &amp;&amp; cur2) {\n            cur-&gt;next = new ListNode(cur1-&gt;val + cur2-&gt;val);\n            cur = cur-&gt;next;\n            cur1 = cur1-&gt;next;\n            cur2 = cur2-&gt;next;\n        }\n\n        while(cur1) {\n            cur-&gt;next = new ListNode(cur1-&gt;val);\n            cur = cur-&gt;next;\n            cur1 = cur1-&gt;next;\n        }\n        while(cur2) {\n            cur-&gt;next = new ListNode(cur2-&gt;val);\n            cur = cur-&gt;next;\n            cur2 = cur2-&gt;next;\n        }\n\n        cur = res-&gt;next;\n        while(cur) {\n            if(cur-&gt;val &gt;= 10) {\n                cur-&gt;val -= 10;\n                if(cur-&gt;next) cur-&gt;next-&gt;val += 1;\n                else cur-&gt;next = new ListNode(1);\n            }\n            cur = cur-&gt;next;\n        }\n\n        return res-&gt;next; \n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/remove-nth-node-from-end-of-list\/\">19. \u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c N \u4e2a\u7ed3\u70b9<\/a>\u2705<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>\u53cc\u6307\u9488<\/strong><\/em><strong><em> + \u865a\u62df\u5934\u8282\u70b9<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>ps\uff1a\u4e00\u822c\u9700\u8981\u5904\u7406\u5934\u8282\u70b9\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u52a0\u4e00\u4e2a\u865a\u62df\u5934\u8282\u70b9 \uff0c\u6bd4\u5982\u5934\u8282\u70b9\u53ef\u80fd\u4e3a\u7a7a\u7684\u60c5\u51b5<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    ListNode* removeNthFromEnd(ListNode* head, int n) {\n        ListNode *dummy = new ListNode();\n        dummy-&gt;next = head;\n        ListNode *cur = dummy;\n        ListNode *pre = dummy;\n\n        while(n--) {\n            pre = pre-&gt;next;\n        } \n        while(pre-&gt;next) {\n            pre = pre-&gt;next;\n            cur = cur-&gt;next;\n        }\n\n        ListNode* tmp = cur-&gt;next;\n        cur-&gt;next = cur-&gt;next-&gt;next;\n        delete(tmp);\n\n        return dummy-&gt;next;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/swap-nodes-in-pairs\/\">24. \u4e24\u4e24\u4ea4\u6362\u94fe\u8868\u4e2d\u7684\u8282\u70b9<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u4e09\u4e2a\u8282\u70b9<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    ListNode* swapPairs(ListNode* head) {\n        ListNode *dummy = new ListNode();\n        dummy-&gt;next = head;\n        ListNode *cur = dummy;\n\n        while(cur-&gt;next &amp;&amp; cur-&gt;next-&gt;next) {\n            ListNode *tmp1 = cur-&gt;next; \/\/\u5b58\u4e0b\u7b2c\u4e00\u4e2a\u8282\u70b9\n            ListNode *tmp2 = cur-&gt;next-&gt;next-&gt;next; \/\/\u5b58\u4e0b\u7b2c\u4e09\u4e2a\u8282\u70b9\n\n            cur-&gt;next = tmp1-&gt;next;\n            cur-&gt;next-&gt;next = tmp1;\n            tmp1-&gt;next = tmp2;\n\n            cur = cur-&gt;next-&gt;next;\n        }\n\n        return dummy-&gt;next;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/reverse-nodes-in-k-group\/\">25. K \u4e2a\u4e00\u7ec4\u7ffb\u8f6c\u94fe\u8868<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u4e0d\u80fd\u7528\u54c8\u5e0c\u5b58\u4e00\u4e0b\u8282\u70b9\u5730\u5740\uff0c\u7136\u540e\u4e24\u4e24\u4ea4\u6362\uff0c\u90a3\u6837\u53ea\u662f\u8282\u70b9\u7684\u4f4d\u7f6e\u53d8\u4e86\uff0c\u4f46\u662fnext\u6307\u9488\u6ca1\u53d8\u5316\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fde\u7ebf\u6ca1\u6709\u53d8\u5316\uff0c\u628a\u7ebf\u626f\u6210\u76f4\u7ebf\u540e\uff0c\u8fd8\u662f1\u21922\u21923<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u5176\u5b9e\u5c31\u662f206.\u53cd\u8f6c\u94fe\u8868\u590d\u6742\u7248\uff0ck\u4e2a\u4e00\u7ec4\u8fdb\u884c\u53cd\u8f6c<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\nprivate:\n    ListNode* myReverse(ListNode* head) {\n        ListNode* pre = nullptr;\n        ListNode* cur = head;\n        ListNode* tmp = nullptr;\n        while(cur) {\n            tmp = cur->next;\n            cur->next = pre;\n            pre = cur;\n            cur = tmp;\n        }\n        return pre;\n    }\npublic:\n    \/\/\u53cd\u8f6c\u94fe\u8868\u590d\u6742\u7248\n    ListNode* reverseKGroup(ListNode* head, int k) {\n        ListNode *dummy = new ListNode();\n        dummy->next = head;\n        ListNode *cur = dummy;\n        int n = k;\n\n        while(cur) {\n            ListNode *first = cur; \/\/first\u6307\u54110\n            \/\/\u5982\u679c\u904d\u5386\u540e\u94fe\u8868\u957f\u5ea6\u4e0d\u8db3k \u5219\u9000\u51fa\n            while(cur &amp;&amp; cur->next &amp;&amp; n) {\n                cur = cur->next;\n                n--;\n            }\n            if(n) break;\n            \/\/cur\u6307\u54113\uff08\u4ee5\u6837\u4f8b2\u4e3e\u4f8b\uff09\n            ListNode *next_node = cur->next; \/\/next_node\u6307\u54114\n            cur->next = nullptr; \/\/\u65ad\u5f00\u8fde\u63a5 \u9632\u6b62\u8c03\u7528\u53cd\u8f6c\u51fd\u6570\u65f6\u8d85\u8fc7k\u8fd8\u4e00\u76f4\u8fd0\u884c\u4e0b\u53bb\n            ListNode *tail = first->next; \/\/tail\u6307\u54111\n            first->next = myReverse(tail); \n            tail->next = next_node; \/\/\u53cd\u8f6c\u540e\u7684\u5c3e\u90e81\u6307\u5411next_node4\n            cur = tail; \/\/\u65b0\u4e00\u8f6e\u7684\u8d77\u59cb\u70b9 \u53734\u7684\u524d\u4e00\u4e2a\u8282\u70b9\n            n = k;\n        }\n         \n        return dummy->next; \n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/copy-list-with-random-pointer\/\">138. \u968f\u673a\u94fe\u8868\u7684\u590d\u5236<\/a>\u2705<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>\u5173\u952e\u662f\u5982\u4f55\u8ba9\u65b0\u7684\u8282\u70b9\u6307\u5411\u65b0\u7684\u8282\u70b9<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\n\/\/ Definition for a Node.\nclass Node {\npublic:\n    int val;\n    Node* next;\n    Node* random;\n    \n    Node(int _val) {\n        val = _val;\n        next = NULL;\n        random = NULL;\n    }\n};\n*\/\n\nclass Solution {\npublic:\n    Node* copyRandomList(Node* head) {\n        Node *cur = head;\n        unordered_map&lt;Node*, Node*&gt; mp;\n\n        while(cur) {\n            mp&#91;cur] = new Node(cur-&gt;val);   \n            cur = cur-&gt;next;\n        }\n\n        cur = head;\n        while(cur) {\n            \/\/\u65b0\u8282\u70b9\u7684next\u6307\u5411\u65e7\u8282\u70b9\u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u65b0\u8282\u70b9\n            mp&#91;cur]-&gt;next = mp&#91;cur-&gt;next]; \n            mp&#91;cur]-&gt;random = mp&#91;cur-&gt;random];\n            cur = cur-&gt;next;\n        }\n\n        return mp&#91;head];  \n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/sort-list\/\">148. \u6392\u5e8f\u94fe\u8868<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u4e0d\u9700\u8981\u7528\u5230\u539f\u8282\u70b9\u7684\u5730\u5740\uff0c\u53ea\u9700\u65b0\u8282\u70b9\u7684val\u503c\u4e0d\u53d8\u5c31\u884c\uff0c\u60f3\u590d\u6742\u4e86<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n *\/\nclass Solution {\npublic:\n    ListNode* sortList(ListNode* head) {\n        ListNode *res = new ListNode();\n        ListNode *cur = head;\n\n        vector&lt;int&gt; v;\n        while(cur) {\n            v.push_back(cur-&gt;val);\n            cur = cur-&gt;next;\n        }\n        ranges::sort(v);\n      \n        cur = res;\n        for(const auto &amp;i : v) {\n            cur-&gt;next = new ListNode(i);\n            cur = cur-&gt;next;\n        }\n\n        return res-&gt;next;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/merge-k-sorted-lists\/\">23. \u5408\u5e76 K \u4e2a\u5347\u5e8f\u94fe\u8868<\/a>\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u4f18\u5148\u961f\u5217\uff08\u5e95\u5c42\u903b\u8f91\u662f\u5806\u6392\u5e8f<\/em><\/strong><strong><em>\uff09<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    struct Comparator { \/\/\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\n        bool operator()(ListNode *a, ListNode *b) {\n            return a->val > b->val; \/\/\u5347\u5e8f \u5373\u5c0f\u6839\u5806\n        }\n    };\n\n    ListNode* mergeKLists(vector&lt;ListNode*>&amp; lists) {\n        priority_queue&lt;ListNode*, vector&lt;ListNode*>, Comparator> pq;\n        \/\/\u904d\u5386\u6bcf\u4e00\u6761\u94fe\u8868\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\n        for(auto &amp;list_cur : lists) { \/\/\u6ce8\u610f\u4e0d\u80fd\u52a0const \u56e0\u4e3alist_cur\u4e00\u76f4\u5728\u6539\u53d8\n            while(list_cur) {\n                pq.push(list_cur);\n                list_cur = list_cur->next;\n            }\n        }\n        \n        ListNode *dummy = new ListNode(0);\n        ListNode *cur = dummy;\n        \/\/\u4ece\u961f\u5217\u4e2d\u4e0d\u65ad\u53d6\u51fa\u8282\u70b9\u5e76\u8fde\u63a5\n        while(!pq.empty()) {\n            cur->next = pq.top();\n            cur = cur->next;\n            pq.pop();\n        }\n        cur->next = nullptr; \/\/\u6807\u5fd7\u94fe\u8868\u7ed3\u675f\n\n        return dummy->next;\n    }\n};<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode.cn\/problems\/lru-cache\/\">146. LRU \u7f13\u5b58<\/a>\u2705\u274c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u5f88\u591a\u7684\u8bed\u6cd5\u7ec3\u4e60\u9898<\/em><\/strong> <em><strong>\u7ec6\u8282<\/strong><\/em><strong><em>\u4e0d\u5c11 \u4f46\u53ef\u60dc\u7528\u5355\u94fe\u8868\u5199\u4e86\u534a\u5929 debug\u53d8\u5929 \u6700\u540e\u51e0\u4e2a\u6837\u4f8b\u8d85\u65f6<\/em><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>\u6700\u540e\u7528STL\u4e2d\u7684 list(\u53cc\u5411\u5faa\u73af\u94fe\u8868)<\/em><\/strong><em><strong> + \u590d\u6742map <\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>\u5f88\u597d\u7684STL\u7ec3\u4e60\u9898<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class LRUCache {\nprivate:\n    int cpy = 0;\n    list&lt;pair&lt;int, int>> m_list; \/\/\u53cc\u5411\u94fe\u8868\u5b58k\u3001v\n    unordered_map&lt;int, list&lt;pair&lt;int, int>>::iterator> mp; \/\/map\u5b58k\u5230list\u8fed\u4ee3\u5668\u7684\u6620\u5c04\npublic:\n    LRUCache(int capacity) {\n        cpy = capacity;\n    }\n    \n    int get(int key) {\n        \/\/\u5982\u679c\u4e0d\u5b58\u5728\n        if(mp.find(key) == mp.end()) return -1;\n        else {\n            \/\/key\u5b58\u5728 \u9700\u8981\u7528splice\u51fd\u6570\u66f4\u65b0key\u5bf9\u5e94\u8282\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u4f4d\u7f6e \n            \/\/\u66f4\u65b0\u5230\u94fe\u8868\u5934\u90e8 \u4ee3\u8868\u6700\u8fd1\u4f7f\u7528\n            m_list.splice(m_list.begin(), m_list, mp&#91;key]);\n            return mp&#91;key]->second;\n        }\n    }\n \n    void put(int key, int value) {\n        \/\/\u5982\u679ckey\u5b58\u5728 \u8c03\u7528get\u5df2\u7ecf\u66f4\u65b0\u5230\u5934\u90e8 \u518d\u66f4\u65b0value\n        if(get(key) != -1) mp&#91;key]->second = value;\n        else { \/\/key\u4e0d\u5b58\u5728\n            if(m_list.size() >= cpy) { \/\/\u5982\u679c\u6ee1\u4e86 \u5148\u4ece\u5c3e\u90e8\u5220\u4e00\u4e2a map\u4e5f\u8981\u5220\n                mp.erase(m_list.back().first);\n                m_list.pop_back();\n            } \n            m_list.push_front(pair(key, value)); \/\/\u5728\u5934\u90e8\u63d2\u5165\u65b0\u5143\u7d20\n            mp&#91;key] = m_list.begin(); \/\/\u628a\u65b0\u5143\u7d20\u5b58\u5230map\u4e2d\n        }\n    }\n};\n\n\/**\n * Your LRUCache object will be instantiated and called as such:\n * LRUCache* obj = new LRUCache(capacity);\n * int param_1 = obj->get(key);\n * obj->put(key,value);\n *\/<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u94fe\u8868\u5b9a\u4e49\u65b9\u5f0f\uff1a STL\u94fe\u8868\uff1alist&lt;int> mylist; \u662f\u4e00\u4e2a\u53cc\u5411\u5faa\u73af\u94fe\u8868\uff0c\u4e0d\u9700\u8981\u5173\u5fc3\u8282\u70b9\u7ba1\u7406 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[18,19,44],"class_list":["post-920","post","type-post","status-publish","format-standard","hentry","category-suanfa","tag-leetcode","tag-19","tag-44"],"_links":{"self":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=920"}],"version-history":[{"count":40,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/920\/revisions"}],"predecessor-version":[{"id":1126,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/920\/revisions\/1126"}],"wp:attachment":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}