<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一.題目描述 ![](https://box.kancloud.cn/2016-01-05_568bb5ee20b58.jpg) ## 二.題目分析 直接按照題目的要求求解,設ListNode *head為待處理的鏈表,算法包括以下步驟:? 1\. 將鏈表head分為前后兩部分,前半部分鏈表head1 和后半部分鏈表head2;? 2\. 將后半段鏈表head12做逆序操作;? 3\. 合并head1, head2; ## 三.示例代碼 ~~~ struct ListNode { int value; ListNode *next; ListNode(int x) : value(x), next(NULL){}; }; class Solution { public: ListNode * reorderList(ListNode *head) { if (head == NULL || head->next == NULL) return head; ListNode *slow = head; ListNode *fast = head; ListNode *cut = head; while (fast && fast->next) { cut = slow; slow = slow->next; fast = fast->next->next; } cut->next = NULL; slow = reverseList(slow); ListNode *result = mergeList(head, slow); return result; } ListNode* reverseList(ListNode *head) // 翻轉鏈表 { if (head == NULL || head->next == NULL) return head; ListNode *prev = head; ListNode *curr = head->next; ListNode *temp = curr; prev->next = NULL; while (curr) { temp = curr->next; curr->next = prev; prev = curr; curr = temp; } return prev; } ListNode* mergeList(ListNode *head1, ListNode *head2) { ListNode* temp1 = head1; ListNode* temp2 = head2; ListNode* pMerge = head1; bool flag = true; while (temp1 != NULL && temp2 != NULL) { if (flag && temp2 != NULL) { temp1 = head1->next; head1->next = temp2; head1 = head1->next; flag = false; } if (!flag && temp1 != NULL) { temp2 = head1->next; head1->next = temp1; head1 = head1->next; flag = true; } } return pMerge; } }; ~~~ 測試結果: ![](https://box.kancloud.cn/2016-01-05_568bb5ee3a2af.jpg) **四.總結** 這道題需要對鏈表進行翻轉操作&對兩個鏈表進行合并操作,實現本身不難,但代碼只是隨便一寫,比較繁瑣,需要后期再優化。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看