给定一个节点数为n的无序单链表,对其按升序排序。
数据范围:,保证节点权值在之内。
要求:空间复杂度 ,时间复杂度
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head node * @return ListNode类 */ #include <stdlib.h> #include <stdio.h> // 快速排序的分区函数 int partition(int arr[], int low, int high) { int pivot = arr[high]; // 选择最后一个元素作为基准 int i = (low - 1); // 小于基准的元素的索引 for (int j = low; j < high; j++) { if (arr[j] < pivot) { // 如果当前元素小于基准 i++; // 增加小于基准的元素索引 // 交换 arr[i] 和 arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // 将基准元素放到正确的位置 int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; // 返回基准元素的索引 } // 快速排序的递归函数 void quickSort(int arr[], int low, int high) { if (low < high) { // 找到分区点 int pi = partition(arr, low, high); // 递归排序分区点两侧的元素 quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } struct ListNode* sortInList(struct ListNode* head ) { // write code here struct ListNode* phead = (struct ListNode*)malloc(sizeof(struct ListNode)); phead->next = head; struct ListNode* p = phead->next; struct ListNode*p1 = p; int minval = head->val, i = 0; int a[100000] = {}; while(p) { a[i] = p->val; p = p->next; i++; } quickSort(a,0 ,i-1); i = 0; while(p1) { p1->val = a[i]; p1 = p1->next; i++; } return phead->next; }取巧的方法,先转换成数组,将其排序后,再放到链表里面,但是此处若是使用冒泡***出现超时。只能通过快速排序法,使时间尽量少。
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head node * @return ListNode类 */ #include <stdlib.h> int lenth(struct ListNode* p){ int num = 0; while(p != NULL){ num++; p = p->next; } return num; } void merge(int* waitsort, int left, int mid, int right){ int i = left, j = mid + 1, k = 0; int* B = (int*)calloc(right - left + 1, sizeof(int)); while(i <= mid && j <= right){ if(waitsort[i] <= waitsort[j]){ B[k++] = waitsort[i++]; }else{ B[k++] = waitsort[j++]; } } while(i <= mid){ B[k++] = waitsort[i++]; } while(j <= right){ B[k++] = waitsort[j++]; } for(i = left, k = 0; i <= right; i++, k++){ waitsort[i] = B[k]; } } void mergeSort(int* waitsort, int left, int right){ int mid; if(left < right){ mid = (left + right) / 2; mergeSort(waitsort, left, mid); mergeSort(waitsort, mid + 1, right); merge(waitsort, left, mid, right); } } struct ListNode* sortInList(struct ListNode* head ) { // write code here struct ListNode* p = head; int len = lenth(p), i, j; int* waitSort = (int*)calloc(len,sizeof(int)); //将链表中的数字放入数组 for(i = 0; i < len; i++){ waitSort[i] = p->val; p = p->next; } mergeSort(waitSort, 0, len - 1); p = head; for(i = 0; i < len; i++){ p->val = waitSort[i]; p = p->next; } return head; }
int cmp(const void*a,const void*b) { return *(int*)a>*(int*)b?1:-1; } struct ListNode* sortInList(struct ListNode* head ) { struct ListNode *p; int *buffer,i=0; if(head==NULL || head->next==NULL) return head; i=0; p=head; while(p!=NULL) { i++; p = p->next; } buffer = (int*)malloc(i*sizeof(int)); i=0; p=head; while(p!=NULL) { buffer[i++] = p->val; p = p->next; } qsort(buffer, i, sizeof(int), cmp); p=head; i=0; while(p!=NULL) { p->val = buffer[i++]; p = p->next; } free(buffer); return head; }
struct ListNode* sortInList(struct ListNode* head) { if (head == NULL || head->next == NULL) { return head; } // Split the list into two halves struct ListNode* slow = head; struct ListNode* fast = head->next; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; } struct ListNode* secondHalf = slow->next; slow->next = NULL; // Sort each half recursively struct ListNode* left = sortInList(head); struct ListNode* right = sortInList(secondHalf); // Merge the sorted halves struct ListNode dummy; struct ListNode* tail = &dummy; while (left != NULL && right != NULL) { if (left->val < right->val) { tail->next = left; left = left->next; } else { tail->next = right; right = right->next; } tail = tail->next; } if (left != NULL) { tail->next = left; } else if (right != NULL) { tail->next = right; } return dummy.next; }
#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); } }
int cmp(void const*a,void const*b){ return *(int*)a-*(int*)b; } struct ListNode* sortInList(struct ListNode* head ) { // write code here int size=0; struct ListNode* p=head; while(p!=NULL){ size++; p=p->next; } p=head; int i=0; int*arr=malloc(sizeof(int)*size); while(p!=NULL){ arr[i]=p->val; i++; p=p->next; } qsort(arr,size,sizeof(int),cmp); p=head; i=0; while(p!=NULL){ p->val =arr[i]; i++; p=p->next; } return head; }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * * @param head ListNode类 the head node * @return ListNode类 */ struct ListNode* sortInList(struct ListNode* head ) { // write code here if (head == NULL) return NULL; struct ListNode *H=malloc(sizeof(struct ListNode)); H->next=head; struct ListNode* pre,*p=head,*r=p->next; p->next = NULL; p=r; while (p) { pre = H; r = p->next; while(pre->next!=NULL&&pre->next->val < p->val) { pre = pre->next; } p->next = pre->next; pre->next = p; p =r; } return H->next; }
void bubble_sort(int arr[],int sz){ int i=0,j=0; for(i=0;i<sz-1;i++){ for(j=0;j<sz-i-1;j++){ if(arr[j]>arr[j+1]){ int tmp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tmp; } } } } struct ListNode* sortInList(struct ListNode* head ) { struct ListNode*tail=head; int cnt=0; while(tail){ tail=tail->next; cnt++; } int *nums=(int *)malloc(sizeof(int)*cnt); tail=head; int index=0; while(tail){ nums[index++]=tail->val; tail=tail->next; } //冒泡排序 bubble_sort(nums,cnt); tail=head; int i=0; //数组转链表 while(tail){ tail->val=nums[i++]; tail=tail->next; } return head; }
int cmp(const void* a,const void* b) { return *(int*)a-*(int*)b; } struct ListNode* sortInList(struct ListNode* head ) { // write code here int count=0; struct ListNode* tail=head; while(tail) { tail=tail->next; count++; } int* arr=(int*)malloc(sizeof(int)*count); int i=0; tail=head; while(tail) { arr[i]=tail->val; tail=tail->next; i++; } qsort(arr, count, sizeof(int), cmp); tail=head; for(int i=0;i<count;i++) { tail->val=arr[i]; tail=tail->next; } return head; }