当前位置:网站首页>One question per day
One question per day
2022-07-25 03:27:00 【Xiao Tang Xuejie】
Give the order the head of a linked list head , Use Insertion sort Sort the list , And back to The header of the sorted linked list .
Insertion sort The steps of the algorithm :
The insertion sort is iterative , Move only one element at a time , Until all elements can form an ordered output list .
In each iteration , Insert sort removes only one element to be sorted from the input data , Find its place in the sequence , And insert it into .
Repeat until all input data is inserted .
The following is a graphical example of the insertion sorting algorithm . Partially sorted list ( black ) Initially, only the first element in the list is included . Each iteration , Delete an element from the input data ( Red ), And insert in place into the sorted list .
Insert and sort the linked list .
Example 1:
Input : head = [4,2,1,3]
Output : [1,2,3,4]
Example 2:
Input : head = [-1,5,3,4,0]
Output : [-1,0,3,4,5]
Tips :
The number of nodes in the list is [1, 5000] Within the scope of
-5000 <= Node.val <= 5000
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode newhead = new ListNode(0);
newhead.next = head;
ListNode pre = newhead; // The previous node of the insertion position
ListNode cur = head; // Current node to insert
ListNode p = newhead, q = cur; //p、q Respectively represent the current node cur Before 、 Back node
while(cur != null) {
q = cur.next;
pre = newhead;
while(pre.next != null && pre.next.val < cur.val) // Find the insertion location
pre = pre.next;
if(pre.next == cur)
p = p.next;
else {
cur.next = pre.next;
pre.next = cur;
p.next = q;
}
cur = q;
}
return newhead.next;
}
}边栏推荐
- Chrome process architecture
- JS common interview questions
- Select sort / cardinality sort
- Optimization of MySQL sorting index fields
- Secondary vocational network security skills competition P100 web penetration test
- Unified return data format
- Li Kou 343 integer partition dynamic programming
- 10. 509 Certificate (structure + principle)
- Moveit2 - 10.urdf and SRDF
- How to use two stacks to simulate the implementation of a queue
猜你喜欢

Advantages and disadvantages of zero trust security

Use and introduction of vim file editor

A code takes you to draw multi format sangjimei pictures such as interactive +pdf+png

A 20 yuan facial cleanser sold tens of thousands in seven days. How did they do it?

The relationship between private domain traffic and fission marketing. What is super app? Can our enterprise own it?

VMware installation

Use of CCleaner

Force deduction brush question 7. Integer inversion

300. Longest increasing subsequence

Direct insert sort / Hill sort
随机推荐
Matplotlib tutorial (I) [getting to know Matplotlib first]
Experience sharing of system architecture designers in preparing for the exam: how to prepare for the exam effectively
What is technical support| Daily anecdotes
Solution: owner's smart site supervision cloud platform
Openlayers ol ext: Transform object, rotate, stretch, zoom in
From input URL to page presentation
Unity: test rotation function
Machine learning exercise 8 - anomaly detection and recommendation system (collaborative filtering)
Brief understanding of operational amplifier
Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer
The difference between abstract classes and interfaces
Easyexcel sets the style of the last row [which can be expanded to each row]
55k is stable, and the recommendation system will always drop God!
[Flink] transform operator map
Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection
292. Nim game
04 -- two ways of writing el and data
Wechat H5 record
Use of CCleaner
[leetcode medium] 34. Find the first and last positions of elements in the sorted array - array double pointer