题目描述
Sort a linked list using insertion sort.
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
Algorithm of Insertion Sort:
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
It repeats until no input elements remain.
Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/insertion-sort-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码
1.以前数组插入排序,无非是两重循环,第一重是遍历整个数组,第二个就是反向循环,找到前面的小于第一循环当前数,并插入。 2.这个是链表无法反向,所以第二重循环就干脆正向遍历一次。找到前面小于第一循环当前数,插入到最后遍历值的前面。 3.首先必须存的临时值,1)比如遍历到的位置为cur,那么要记住cur前面的pre,用于发生插入时,要接到cur->next上。2)要记住满足条件的(相当于第二层遍历)reveserCu > cur,所以交换这两个值,需要保存reverseCur前面的reversePre。 4.因此出现了两种情况。第一种发生了插入,那么每次第一重循环的pre不变还是当前那个值(自动移位了不要管);第二种没发生插入,那么第一重循环的pre要变成下一个,也就是cur。
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(head == NULL || head->next == NULL){
return head;
}
ListNode *dummyHead =new ListNode(0);
dummyHead->next = head;
ListNode *cur = head->next;
ListNode *pre = head;
while(cur){
ListNode *reverseCur = dummyHead->next;
ListNode *reversePre = dummyHead;
ListNode *next = cur->next;
while(reverseCur != cur){
if (reverseCur->val > cur->val ){
// cur插入到这个reverserCur前面
pre->next = cur->next;
reversePre->next = cur;
cur->next = reverseCur;
break;
}
reversePre = reverseCur;
reverseCur = reverseCur->next;
}
if (reverseCur == cur){
//如果是到结束还没有插入,那么pre就换个新的,换到cur位置
pre = cur;
}
cur = next;
}
return dummyHead->next;
}
};
其他
#include <iostream>
using namespace std;
/// Ad-Hoc
/// Time Complexity: O(n^2)
/// Space Compelxity: O(1)
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* createLinkedList(int arr[], int n){
if(n == 0)
return NULL;
ListNode* head = new ListNode(arr[0]);
ListNode* curNode = head;
for(int i = 1 ; i < n ; i ++){
curNode->next = new ListNode(arr[i]);
curNode = curNode->next;
}
return head;
}
void printLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode != NULL){
cout << curNode->val << " -> ";
curNode = curNode->next;
}
cout << "NULL" << endl;
return;
}
void deleteLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode != NULL){
ListNode* delNode = curNode;
curNode = curNode->next;
delete delNode;
}
return;
}
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* dummyHead = new ListNode(-1);
dummyHead->next = head;
ListNode* pre = dummyHead->next;
while(pre->next){
int val = pre->next->val;
ListNode* next = pre->next->next;
ListNode* pi = dummyHead;
for(; pi != pre; pi = pi->next)
if(pi->next->val > val){
ListNode* pj = pi->next;
ListNode* swapNode = pre->next;
pi->next = swapNode;
swapNode->next = pj;
pre->next = next;
break;
}
if(pi == pre) pre = pre->next;
// printLinkedList(dummyHead);
}
return dummyHead->next;
}
};
int main() {
int arr1[4] = {4, 2, 1, 3};
ListNode* head1 = createLinkedList(arr1, 4);
ListNode* res1 = Solution().insertionSortList(head1);
printLinkedList(res1);
deleteLinkedList(res1);
int arr2[5] = {-1, 5, 3, 4, 0};
ListNode* head2 = createLinkedList(arr2, 5);
ListNode* res2 = Solution().insertionSortList(head2);
printLinkedList(res2);
deleteLinkedList(res2);
return 0;
}