> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/148.-sort-list-medium.md).

# 148. Sort List

## 1.問題

* 排序, 時間複雜度為O(n logn), 空間複雜度為constant

![](/files/-LI03z2UMfWNTTy3G8Cx)

## 2.想法&#x20;

* Divide and conquer, Binary sort
  * 將List分為前後兩段並排序

## 3.程式碼

```
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) {
            return head;
        }
        
        //Split the list
        ListNode* prev = NULL;
        ListNode* firstHalf = head;
        ListNode* secondHalf = head;
        while (secondHalf && secondHalf->next) {
            prev = firstHalf;
            firstHalf = firstHalf->next;
            secondHalf = secondHalf->next->next;
        }
        
        //The head of the scend half one
        ListNode* mid = firstHalf;
        prev->next = NULL;
        
        //Divid and conquer
        ListNode* l = sortList(head);
        ListNode* r = sortList(mid);
        
        //Combine
        return mergeList(l, r);
    }
private: 
    ListNode* mergeList(ListNode* l1, ListNode* l2){
        ListNode* newNode = new ListNode(0);
        ListNode* curr = newNode;
        //traversal 2 list at the same time
        while (l1 && l2) {
            if (l1->val <= l2->val) {
                curr->next = l1;
                //move
                l1 = l1->next;
            } else {
                curr->next = l2;
                //move
                l2 = l2->next;
            }
            //move
            curr = curr->next;
        }
        
        //attach remained list
        if (l1) {
            curr->next = l1; 
        }
        if (l2) {
            curr->next = l2; 
        }
        return newNode->next;
    }
};
```

## 4.Performance

![](/files/-LI040fwQ73cDBQihnCb)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/148.-sort-list-medium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
