ListNode * HandleIt(queue<ListNode*>& ltr, stack<ListNode*>& rtl) { ListNode* p; if (ltr.front()->val > rtl.top()->val) { p = rtl.top(); rtl.pop(); } else { p = ltr.front(); ltr.pop(); } return p; } ListNode* sortLinkedList(ListNode* head) { if (!head->next) return head; //获得两条递增的线性表 queue<ListNode*> ltr;//从左向右递增 stack<ListNode*> rtl;//从右向左递增 bool isOdd = true;//是否奇数 while (head) { if (isOdd) { ltr.emplace(head); } else { rtl.emplace(head); } isOdd = !isOdd; head = head->next; } //找出新的链头 ListNode* newHead = HandleIt(ltr, rtl); ListNode* p = newHead; //对两个线性表进行整合排序 while (!ltr.empty() || !rtl.empty()) { if (ltr.empty()) { p->next = rtl.top(); rtl.pop(); } else if (rtl.empty()) { p->next = ltr.front(); ltr.pop(); } else { p->next = HandleIt(ltr, rtl); } p = p->next; } p->next = nullptr; return newHead; }