/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: ListNode* mergeList(ListNode* head1,ListNode* head2) { if(!head1 || !head2) return head1 ? head1 : head2; ListNode* head=new ListNode(0); ListNode* cur=head; while(head1 && head2) { if(head1->val <= head2->val){ cur->next = head1; head1=head1->next; }else{ cur->next = head2; head2=head2->next; } cur=cur->next; } if(head1) { cur->next = head1; } else { cur->next = head2; } return head->next; } ListNode* sortInList(ListNode* head) { if(head==nullptr || !head->next) return head; ListNode* left=head ,*mid=head, *right=head; while(right && right->next) { left = mid; mid=mid->next; right = right->next->next; } left->next = nullptr; ListNode* left_ok = sortInList(head); ListNode* right_ok = sortInList(mid); return mergeList(left_ok,right_ok); } };