147:对链表进行插入排序
This commit is contained in:
parent
a6ff230d58
commit
3ce3e2ab3f
88
src/main/java/leetcode/editor/cn/InsertionSortList.java
Normal file
88
src/main/java/leetcode/editor/cn/InsertionSortList.java
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
//对链表进行插入排序。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。
|
||||||
|
//每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 插入排序算法:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
|
||||||
|
// 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
|
||||||
|
// 重复直到所有输入数据插入完为止。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: 4->2->1->3
|
||||||
|
//输出: 1->2->3->4
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: -1->5->3->4->0
|
||||||
|
//输出: -1->0->3->4->5
|
||||||
|
//
|
||||||
|
// Related Topics 排序 链表
|
||||||
|
// 👍 396 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.ListNode;
|
||||||
|
|
||||||
|
//147:对链表进行插入排序
|
||||||
|
public class InsertionSortList{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new InsertionSortList().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 insertionSortList(ListNode head) {
|
||||||
|
if (head == null || head.next == null) {
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
ListNode current = head.next;
|
||||||
|
head.next = null;
|
||||||
|
while (current != null) {
|
||||||
|
ListNode temp = head;
|
||||||
|
ListNode next = current.next;
|
||||||
|
current.next = null;
|
||||||
|
if (current.val < temp.val) {
|
||||||
|
head = current;
|
||||||
|
head.next = temp;
|
||||||
|
} else {
|
||||||
|
while (temp.next != null) {
|
||||||
|
if (current.val < temp.next.val) {
|
||||||
|
current.next = temp.next;
|
||||||
|
temp.next = current;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
temp.next = temp.next == null ? current : temp.next;
|
||||||
|
}
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
30
src/main/java/leetcode/editor/cn/InsertionSortList.md
Normal file
30
src/main/java/leetcode/editor/cn/InsertionSortList.md
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<p>对链表进行插入排序。</p>
|
||||||
|
|
||||||
|
<p><img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif"><br>
|
||||||
|
<small>插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。<br>
|
||||||
|
每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。</small></p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>插入排序算法:</strong></p>
|
||||||
|
|
||||||
|
<ol>
|
||||||
|
<li>插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。</li>
|
||||||
|
<li>每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。</li>
|
||||||
|
<li>重复直到所有输入数据插入完为止。</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> 4->2->1->3
|
||||||
|
<strong>输出:</strong> 1->2->3->4
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong> -1->5->3->4->0
|
||||||
|
<strong>输出:</strong> -1->0->3->4->5
|
||||||
|
</pre>
|
||||||
|
<div><div>Related Topics</div><div><li>排序</li><li>链表</li></div></div>\n<div><li>👍 396</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user