Leetcode 147. Insertion Sort List(链表实现快排)
来源:互联网 发布:知乎关注最多的问题 编辑:程序博客网 时间:2024/06/05 08:22
147. Insertion Sort List
Total Accepted: 66609 Total Submissions: 230331 Difficulty: Medium
Sort a linked list using insertion sort.
Subscribe to see which companies asked this question
Have you met this question in a real interview?
链接: https://leetcode.com/problems/insertion-sort-list/
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<stack>#include<queue>#include<cmath>#include<map>#include<set>typedef long long ll;#define eps 1e-8#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define bug printf("hi"\n)using namespace std;#define INF 0x3f3f3f3f#define N 30struct ListNode{int val;ListNode *next;ListNode(int x):val(x),next(NULL){}};void show(ListNode * head){while(head){printf("%d ",head->val);head=head->next;}printf("\n");}class Solution{public:ListNode *insertionSortList(ListNode * head){if(head==NULL||head->next==NULL) return head;ListNode* pre=new ListNode(0);quicksort(pre,head,NULL);head=pre->next;delete pre;return head;}ListNode * quicksort(ListNode* pre,ListNode * head,ListNode *tail){if(head==tail) return head;if(head->next==NULL) return head;ListNode * mid=getpos(pre,head,tail);quicksort(pre,pre->next,mid);quicksort(mid,mid->next,tail);return pre;}ListNode * getpos(ListNode* pre,ListNode *head,ListNode * tail){ListNode *small=new ListNode(0),*big=new ListNode(0);ListNode *ss=small,*bb=big;for(ListNode * it=head->next;it!=tail;it=it->next)if(it->val<=head->val){small->next=it;small=it;} else { big->next=it; big=it; }big->next=tail;head->next=bb->next;small->next=head;pre->next=ss->next;delete bb;delete ss;return head;}};int main(){int i,j;ListNode * head=new ListNode(3);ListNode * tail=head;for(int i=2;i>=1;i--){ListNode * temp=new ListNode(i);tail->next=temp;tail=temp;}show(head);Solution ans;tail=ans.insertionSortList(head);show(tail);return 0;}
0 0
- Leetcode 147. Insertion Sort List(链表实现快排)
- Leetcode: Sort List - 快排
- LeetCode 147. Insertion Sort List(插入有序链表)
- leetcode 147. Insertion Sort List-链表插入排序
- LeetCode-147. Insertion Sort List (JAVA)链表插入排序
- leetcode 147. Insertion Sort List 链表插入排序
- LeetCode 147. Insertion Sort List
- [LeetCode]147.Insertion Sort List
- [leetcode] 147.Insertion Sort List
- 147. Insertion Sort List LeetCode
- LeetCode 147. Insertion Sort List
- LeetCode *** 147. Insertion Sort List
- Leetcode 147. Insertion Sort List
- leetcode.147. Insertion Sort List
- leetcode 147. Insertion Sort List
- LeetCode-147.Insertion Sort List
- LeetCode - 147. Insertion Sort List
- [leetcode] 147. Insertion Sort List
- ML for hackers 第一章笔记
- 关于排序的几种练习.
- 图像增强算法实现--图像的椒盐噪声、随机噪声、高斯噪声、均值滤波、中值滤波以及高斯滤波
- Bootstrap的一个很漂亮的web万能模板
- AlphaGo 开源项目研究(2)
- Leetcode 147. Insertion Sort List(链表实现快排)
- android weight(权重)的具体分析
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- 凭啥Java的运行环境称虚拟机,Python的只能称解释器
- 今年第一次面试
- Linux 设备驱动中的 I/O模型(二)—— 异步通知和异步I/O
- Leetcode Best Time to Buy and Sell Stock III
- grep排除搜索的目录
- 视频编解码学习之二:数字视频