> For the complete documentation index, see [llms.txt](https://leetcodebook-1.gitbook.io/top-interview-150/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leetcodebook-1.gitbook.io/top-interview-150/divide-and-conquer/sort-list.md).

# 排序链表

题目链接: <https://leetcode.cn/problems/sort-list>

## 解题思路：

1. 将队列从中间一分为二，分割方法用两个指针指向链表头，一个向后遍历一个元素一个向后遍历两个元素，当遍历到尾部时，另一个指针指向的链表就是后半部分的头元素
2. 对拆分后的链表继续拆分，直至到单个元素，再两两进行排序合并，由小到大升序合并，得出多个相对有序链表
3. 合并到最后，将两个相对有序的链表合并成一个完整的有序链表并返回

```go
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func sortList(head *ListNode) *ListNode {
	return split(head, nil)
}

func split(head, end *ListNode) *ListNode {
	if head == nil {
		return head
	}
	if head.Next == end {
		head.Next = nil
		return head
	}
	slow, fast := head, head
	for fast != end {
		slow = slow.Next
		fast = fast.Next
		if fast != end {
			fast = fast.Next
		}
	}
	mid := slow
	return merge(split(head, mid), split(mid, end))
}

func merge(head1, head2 *ListNode) *ListNode {
	rootHead := &ListNode{}
	tmp, l1, l2 := rootHead, head1, head2
	for l1 != nil && l2 != nil {
		if l1.Val < l2.Val {
			tmp.Next = l1
			l1 = l1.Next
		} else {
			tmp.Next = l2
			l2 = l2.Next
		}
		tmp = tmp.Next
	}
	if l1 != nil {
		tmp.Next = l1
	} else if l2 != nil {
		tmp.Next = l2
	}
	return rootHead.Next
}
```

## 复杂度分析

* **时间复杂度：** 时间复杂度为$$O(n\*log n)$$，$$n$$为链表长度
* **空间复杂度：** 空间复杂度为$$O(log n)$$，$$n$$为链表长度


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://leetcodebook-1.gitbook.io/top-interview-150/divide-and-conquer/sort-list.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
