doc: 2025-10-24打卡
This commit is contained in:
42
147.insertion-sort-list.java
Normal file
42
147.insertion-sort-list.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* @lc app=leetcode id=147 lang=java
|
||||
*
|
||||
* [147] Insertion Sort List
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* 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) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
dummy.next = null;
|
||||
ListNode t;
|
||||
while(head != null) {
|
||||
t = head;
|
||||
head = head.next;
|
||||
ListNode prev = dummy;
|
||||
while(true) {
|
||||
if(prev.next == null || prev.next.val > t.val) {
|
||||
t.next = prev.next;
|
||||
prev.next = t;
|
||||
break;
|
||||
} else {
|
||||
prev = prev.next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dummy.next;
|
||||
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user