148:排序链表
This commit is contained in:
parent
3ce3e2ab3f
commit
b09e904694
94
src/main/java/leetcode/editor/cn/SortList.java
Normal file
94
src/main/java/leetcode/editor/cn/SortList.java
Normal file
@ -0,0 +1,94 @@
|
||||
//给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。
|
||||
//
|
||||
// 进阶:
|
||||
//
|
||||
//
|
||||
// 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:head = [4,2,1,3]
|
||||
//输出:[1,2,3,4]
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:head = [-1,5,3,4,0]
|
||||
//输出:[-1,0,3,4,5]
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:head = []
|
||||
//输出:[]
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 链表中节点的数目在范围 [0, 5 * 104] 内
|
||||
// -105 <= Node.val <= 105
|
||||
//
|
||||
// Related Topics 排序 链表
|
||||
// 👍 1168 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.ListNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
//148:排序链表
|
||||
public class SortList{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new SortList().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode sortList(ListNode head) {
|
||||
List<ListNode> list = new ArrayList<>();
|
||||
while (head != null) {
|
||||
list.add(head);
|
||||
head = head.next;
|
||||
}
|
||||
Collections.sort(list, (n1, n2) -> n1.val - n2.val);
|
||||
int size = list.size();
|
||||
if (size == 0) {
|
||||
return null;
|
||||
}
|
||||
head = list.get(0);
|
||||
ListNode temp = head;
|
||||
for (int i = 1; i < size; i++) {
|
||||
temp.next = list.get(i);
|
||||
temp.next.next = null;
|
||||
temp = temp.next;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
40
src/main/java/leetcode/editor/cn/SortList.md
Normal file
40
src/main/java/leetcode/editor/cn/SortList.md
Normal file
@ -0,0 +1,40 @@
|
||||
<p>给你链表的头结点 <code>head</code> ,请将其按 <strong>升序</strong> 排列并返回 <strong>排序后的链表</strong> 。</p>
|
||||
|
||||
<p><b>进阶:</b></p>
|
||||
|
||||
<ul>
|
||||
<li>你可以在 <code>O(n log n)</code> 时间复杂度和常数级空间复杂度下,对链表进行排序吗?</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" />
|
||||
<pre>
|
||||
<b>输入:</b>head = [4,2,1,3]
|
||||
<b>输出:</b>[1,2,3,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" />
|
||||
<pre>
|
||||
<b>输入:</b>head = [-1,5,3,4,0]
|
||||
<b>输出:</b>[-1,0,3,4,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>head = []
|
||||
<b>输出:</b>[]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<ul>
|
||||
<li>链表中节点的数目在范围 <code>[0, 5 * 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>排序</li><li>链表</li></div></div>\n<div><li>👍 1168</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user