给定一个节点数为n的无序单链表,对其按升序排序。
数据范围:
,保证节点权值在
之内。
要求:空间复杂度
,时间复杂度 )
参考大佬的代码做了一点总结
public ListNode sortInList (ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode move = head;
while (move.next != null) {
ListNode temp = move.next;
while (temp != null) {
if (temp.val < move.val) {
int val = temp.val;
temp.val = move.val;
move.val = val;
}
temp = temp.next;
}
move = move.next;
}
return head;
} public ListNode sortInList (ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode sorted = dummy;
while (sorted.next != null) {
ListNode pre = sorted;
ListNode cur = sorted.next;
ListNode preMin = null;
ListNode min = null;
while (cur != null) {
if (min == null || cur.val < min.val) {
preMin = pre;
min = cur;
}
pre = pre.next;
cur = cur.next;
}
preMin.next = min.next;
min.next = sorted.next;
sorted.next = min;
sorted = min;
}
return dummy.next;
} public ListNode sortInList (ListNode head) {
if (head == null || head.next == null) {
return head;
}
return mergeSort(head);
}
public ListNode mergeSort(ListNode head) {
if (head.next == null) {
return head;
}
ListNode slow = head, fast = head, pre = null;
while (fast != null && fast.next != null) {
pre = slow;
slow = slow.next;
fast = fast.next.next;
}
pre.next = null;
ListNode left = mergeSort(head);
ListNode right = mergeSort(slow);
return merge(left, right);
}
public ListNode merge(ListNode left, ListNode right) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (left != null && right != null) {
if (left.val < right.val) {
cur.next = left;
left = left.next;
} else {
cur.next = right;
right = right.next;
}
cur = cur.next;
}
if (left != null) {
cur.next = left;
}
if (right != null) {
cur.next = right;
}
return dummy.next;
}
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
// write code here
ArrayList<Integer> arr=new ArrayList<>();
while (head!=null){
arr.add(head.val);
head=head.next;
}
Collections.sort(arr);
ListNode ans=new ListNode(0);
ListNode cur=ans;
// ListNode node=ans;
for (int i=0;i<arr.size();i++){
cur.next=new ListNode(arr.get(i));
cur=cur.next;
}
return ans.next;
}
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
// 寻找最小的元素,并利用头插法插入到节点
ListNode dummy = new ListNode(Integer.MAX_VALUE);
dummy.next = head;
ListNode sorted = dummy;
while (sorted.next != null) {
ListNode pre = sorted;
ListNode cur = sorted.next;
ListNode pre_min = null;
ListNode min = null;
// 寻找最小的数
while (cur != null) {
if (min == null || cur.val < min.val) {
min = cur;
pre_min = pre;
}
// 继续向后移动指针
cur = cur.next;
pre = pre.next;
}
// 利用头插法插入
pre_min.next = min.next;
min.next = sorted.next;
sorted.next = min;
// 哨兵节点往后移动
sorted = min;
}
return dummy.next;
}
} import java.util.*;
public class Solution {
public ListNode sortInList (ListNode head) {
ArrayList<Integer> al = new ArrayList<Integer>();
ListNode p = head;
while (p != null) {
al.add(p.val);
p = p.next;
}
p = head;
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
p.val = al.get(i);
p = p.next;
}
return head;
}
} void quickSorted(int *nums, int left, int right){
if (left > right) return;
int i = left, j = right, temp = nums[left];
while (i < j){
while (i < j && temp <= nums[j])
j--;
if (i < j)
nums[i++] = nums[j];
while (i < j && temp > nums[i])
i++;
nums[j--] = nums[i];
}
nums[i] = temp;
quickSorted(nums, left, i - 1);
quickSorted(nums, i + 1, right);
}
struct ListNode* sortInList(struct ListNode* head ) {
int length = 0;
struct ListNode *cur = head;
while(cur){
length++;
cur = cur->next;
}
int *nums = (int*)malloc(sizeof(int) * length);
cur = head;
int index = 0;
//链表转数组
while (cur){
nums[index++] = cur->val;
cur = cur->next;
}
quickSorted(nums, 0, length - 1);
cur = head;
int i = 0;
//数组重新转链表
while (cur){
cur->val = nums[i++];
cur = cur->next;
}
return head;
} class Solution {
public:
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
//合并两个有序链表。(第一次合并链表长度为1,当然是有序的,然后之后都是有序合并)
ListNode* merge(ListNode*left,ListNode*right){
if(!left)return right;
if(!right)return left;
if(left == right)return left;
//思路是将right有序插入left中,给left设置一个头节点
unique_ptr<ListNode> dummpy(new ListNode(0));
dummpy->next = left;
ListNode*pre = dummpy.get();//left的前一节点
while(left && right){
//如果左大于右,就把右指针插入左指针左边,然后右指针右移
if(left->val > right->val){
ListNode*rightNext = right->next;
pre->next = right;
right->next = left;
pre = right;
right = rightNext;
}
//否则左指针左移
else{
pre = left;
left = left->next;
}
}
//如果right还有数据,就接到left末端。此时right的数据都大于left
if(right){
pre->next = right;
}
return dummpy->next;
}
ListNode* sortInList(ListNode* head) {
// write code here
if(!head)return head;
vector<tuple<ListNode*,ListNode*>> toSearch;
ListNode*cur = head;
ListNode*remain = nullptr;//如果链表长度为奇数,记录最后一个节点
//两个分成一组
while(cur != nullptr){
if(cur->next != nullptr){
toSearch.emplace_back(cur,cur->next);
ListNode*next = cur->next->next;
cur->next->next = nullptr;
cur->next = nullptr;
cur = next;
}
else{
remain = cur;
break;
}
}
//记录以排序的
vector<ListNode*>hasSearched;
while(true){
while(toSearch.empty() == false){
auto [left,right] = toSearch.back();
toSearch.pop_back();
//从待排序中取出一组节点,归并后放入已排序
hasSearched.emplace_back(merge(left,right));
}
//如果长度为奇数,单独的,就和已排序的最后一个合并
if(remain){
if(hasSearched.empty() == false){
hasSearched.back() = merge(hasSearched.back(), remain);
}
else{
hasSearched.emplace_back(remain);
}
remain = nullptr;
}
//如果合并玩只剩一个,就排好了
if(hasSearched.size() == 1){
break;
}
else{
toSearch.clear();
//如果奇数长,就记录最后一个为remain
if(hasSearched.size()&1){
remain = hasSearched.back();
hasSearched.pop_back();
}
//两个为一组
for(int i=0;i<hasSearched.size();i+=2){
toSearch.emplace_back(hasSearched[i],hasSearched[i+1]);
}
///清空,然后返回循环开始继续合并
hasSearched.clear();
}
}
return hasSearched.back();
}
}; import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
// write code here
// 递归推出条件:如果当前节点为空或者后面没有节点了,返回节点
if(head == null || head.next == null) return head;
ListNode quick = head.next;
ListNode slow = head;
while(true){
if(quick == null || quick.next == null) break;
quick = quick.next.next;
slow = slow.next;
}
// 从中间切割链表
ListNode temp = slow.next;
slow.next = null;
// 左右两边分别递归
ListNode left = sortInList(head);
ListNode right = sortInList(temp);
ListNode pre = new ListNode(0);
ListNode cur = pre;
// 排序操作
while(left != null && right != null){
if(left.val < right.val){
pre.next = left;
left = left.next;
}else{
pre.next = right;
right = right.next;
}
pre = pre.next;
}
pre.next = left == null ? right : left;
return cur.next;
}
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
static bool cmp(ListNode* a,ListNode* b){
return a->val < b->val;
}
ListNode* sortInList(ListNode* head) {
// write code here
vector<ListNode*> nums;
while(head != nullptr){
nums.push_back(head);
head = head->next;
}
sort(nums.begin(), nums.end() ,cmp);
for(int i = 0; i < nums.size(); i++){
nums[i]->next = nums[i+1];
}
nums[nums.size()-1]->next = nullptr;
return nums[0];
}
}; import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
// write code here
return mergerSort(head);
}
public ListNode mergerSort(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode slow = head;
ListNode fast = head.next.next;
ListNode left, right;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode next = slow.next;
// slow
// mid next
// 1 3 4 [5] | 6 7 4 2
slow.next = null;
left = mergerSort(head);
right = mergerSort(next);
return mergeList(left, right);
}
// dh ->
// 1 2 3 4
// 6 7 8 9
public ListNode mergeList(ListNode left, ListNode right){
ListNode dummyHead = new ListNode(0);
ListNode cur = dummyHead;
//ListNode cur = left.val < right.val ? left : right;
while(left != null && right != null){
if(left.val < right.val){
cur.next = left;
left = left.next;
}
else{
cur.next = right;
right = right.next;
}
cur = cur.next;
}
cur.next = left == null ? right : left;
return dummyHead.next;
}
} `
class Solution {
public:
ListNode* sortInList(ListNode* head) {
multiset<int>myset;//multiset允许重复元素,而set 则会覆盖重复元素
ListNode* p=head;
while(p!=nullptr){
myset.insert(p->val);
p=p->next;
}
p=head;
for(auto it=myset.begin();it!=myset.end();it++){
p->val=*it;
p=p->next;
}
return head;;
}};
`
#include <stdlib.h>
int privot(int a[], int low, int high);
void quicksort(int a[], int low, int high);
struct ListNode* sortInList(struct ListNode* head ) {
// write code here
int s[100000] = {0};
int i = 0;
struct ListNode *p = head;
//将每个节点的val存入数组s中
while (p) {
s[i++] = p->val;
p = p->next;
}
//快排
quicksort(s, 0, i-1);
p = head;
i = 0;
//重新给链表赋值val
while (p) {
p->val = s[i++];
p = p->next;
}
return head;
}
//划分
int privot(int a[], int low, int high){
int temp = a[low];
while (low < high) {
while (low < high && a[high] >= temp) high--;
a[low] = a[high];
while (low < high && a[low] <= temp) low++;
a[high] = a[low];
}
a[low] = temp;
return low;
}
//快排
void quicksort(int a[], int low, int high){
if (low < high) {
int pri = privot(a, low, high);
quicksort(a, low, pri-1);
quicksort(a, pri+1, high);
}
} public ListNode sortInList (ListNode head) {
// write code here
ListNode cur=head;
List<Integer> list=new ArrayList<>();
while(cur!=null){
list.add(cur.val);
cur=cur.next;
}
Collections.sort(list);
ListNode fake=new ListNode(1);
ListNode last=fake;
for(int i=0;i<list.size();i++){
ListNode node=new ListNode(list.get(i));
last.next=node;
last=node;
}
return fake.next;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
ListNode* sortInList(ListNode* head) {
// 时间复杂度O(NlogN),空间复杂度O(logN)
if (head == NULL || head->next == NULL) return head;
ListNode* left = head, *pre = NULL, *mid = head, *right = head;
while (right && right->next) {
pre = mid;
mid = mid->next;
right = right->next->next;
}
pre->next = NULL;
return merge(sortInList(left), sortInList(mid));
}
ListNode* merge(ListNode* p1, ListNode* p2) {
ListNode* dummy = new ListNode(-1), *cur = dummy;
while (p1 && p2) {
if (p1->val < p2->val) {
cur->next = p1;
p1 = p1->next;
} else {
cur->next = p2;
p2 = p2->next;
}
cur = cur->next;
}
if (p1) cur->next = p1;
if (p2) cur->next = p2;
return dummy->next;
}
}; /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
ListNode* sortInList(ListNode* head) {
// write code here
vector <int> v;
ListNode *p=head;
while (p) {
v.push_back(p->val);
p=p->next;
}
p=head;
priority_queue<int,vector<int>,greater<int>> q;//!!!priority_queue必须基于容器
for (int i=0;i<v.size();i++) {
while (p) {
q.push(p->val);
p=p->next;
}
}
p=head;javascript:void(0);
while (!q.empty()) {
p->val=q.top();
q.pop();
p=p->next;
}
return head;
}
}; class Solution {
public:
ListNode* sortInList(ListNode* head) {
// 1.
vector<ListNode*> nums;
while (head) {
nums.emplace_back(head);
head = head->next;
}
// 2.
std::sort(nums.begin(), nums.end(), [](ListNode* l1, ListNode* l2) {
return l1->val <= l2->val;
});
// 3.
int n = nums.size();
for (int i = 0; i < n - 1; ++i) {
nums[i]->next = nums[i + 1];
}
nums[n - 1]->next = nullptr;
return nums[0];
}
};