-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path148.h
46 lines (42 loc) · 1.22 KB
/
148.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Created by 17336 on 2022/3/4.
//
#ifndef HOT100_148_H
#define HOT100_148_H
#include "linknode.h"
#include <algorithm>
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (head == nullptr || head->next == nullptr) return head;
//对于单链表排序,冒牌排序比较适合
ListNode *p, *end = nullptr;
bool flag = true;
//单链表不能随机访问,所以用end记录已经有序的后半段的起点
while (head->next != end) {
p = head;
//每次冒泡到end就结束,如果本次冒泡一个也没交换,说明已经有序
while (p->next != end) {
if (p->val > p->next->val) {
std::swap(p->val, p->next->val);
flag = false;
}
p = p->next;
}
if (flag) return head;
end = p;
}
return head;
}
};
#endif //HOT100_148_H