博客
关于我
Algorithms : Sort linklist
阅读量:366 次
发布时间:2019-03-04

本文共 4373 字,大约阅读时间需要 14 分钟。

Sort a linked list in O(n log n) time using constant space complexity.

 

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* sortList(ListNode* head) {        if (head == nullptr || head->next == nullptr) return head;                ListNode * pivot = head, * small = nullptr, * large = nullptr, *sp = nullptr, *lp = nullptr;        head = head->next;        while(head) {            if(head->val < pivot->val) {                if (small == nullptr) small = head;                else sp->next = head;                sp = head;            } else {                if (large == nullptr) large = head;                else lp->next = head;                lp = head;            }            head = head->next;        }        if(sp) sp->next = nullptr;        if(lp) lp->next = nullptr;                // sort the sub list.        sp = sortList(small);        lp = sortList(large);                // merge the sub list.        if(sp)  {            head = sp;            // seek to the last node.            while(sp->next)                sp = sp->next;            sp->next = pivot;        } else {            head = pivot;        }                pivot->next = lp;        return head;    }};

if we only change the value. it will improve a some time complexity.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private:    ListNode * partition(ListNode * head, ListNode * tail) {        if(head == tail || head->next == nullptr) return head;        int pivot = head->val;        ListNode * slow = head, *fast = head->next;        while(fast && fast != tail) {            if (fast->val < pivot) {                slow = slow->next;                swap(slow->val, fast->val);            }            fast = fast->next;        }        // swap the pivot        swap(slow->val, head->val);        return slow;    }        void quicksort(ListNode * head, ListNode * tail) {        if(head == tail || head->next == nullptr) return;                ListNode * pivot = partition(head, tail);        quicksort(head, pivot);        quicksort(pivot->next, tail);    }public:    ListNode* sortList(ListNode* head) {        quicksort(head, nullptr);        return head;    }};

 

if we do a non-recursive merge sort. the effeciency will be time O(NlogN), and the space with O(1).

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private:    // from head->@->@->@->null to    // head->@->@->null  current->@->null    ListNode * cut(ListNode * head, int n) {        if (head == nullptr) return head;        ListNode * p = head;        while(--n && p) {            p = p->next;        }        if (p == nullptr) return nullptr;        ListNode * next = p->next;        // cut the current node        p->next = nullptr;        return next;    }        // merge two linklist and return the header    ListNode * merge(ListNode * l, ListNode * r) {        ListNode dummyHead(0);        ListNode * p = &dummyHead;                while(l && r) {            if(l->val < r->val) {                p->next = l;                p = l;                l = l->next;            } else {                p->next = r;                p = r;                r = r->next;            }        }        p->next = l ? l : r;        return dummyHead.next;    }    public:    ListNode* sortList(ListNode* head) {        ListNode dummyHead(0);        dummyHead.next = head;                int length = 0;        while(head) {            head = head->next;            ++length;        }                for(int size = 1; size < length; size <<= 1) {            ListNode * cur = dummyHead.next;            ListNode * p = &dummyHead;                        while(cur) {                ListNode * left = cur;                ListNode * right = cut(cur, size);                cur = cut(right, size);                                p->next = merge(left, right);                // seek to the end.                while(p->next) {                    p = p->next;                }                            }        }                return dummyHead.next;            }};

 

转载地址:http://upbg.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(71)——数据库介绍(MySQL安装 体系结构、基本管理)再回顾
查看>>
Mysql学习总结(72)——MySQL 开发者开发,设计规范再总结
查看>>
Mysql学习总结(73)——MySQL 查询A表存在B表不存在的数据SQL总结
查看>>
Mysql学习总结(74)——慢SQL!压垮团队的最后一根稻草!
查看>>
Mysql学习总结(75)——并发量大、数据量大的互联网业务数据库设计军规
查看>>
Mysql学习总结(76)——MySQL执行计划(explain)结果含义总结
查看>>
Mysql学习总结(77)——温故Mysql数据库开发核心原则与规范
查看>>
Mysql学习总结(78)——MySQL各版本差异整理
查看>>
Mysql学习总结(79)——MySQL常用函数总结
查看>>
Mysql学习总结(7)——MySql索引原理与使用大全
查看>>
Mysql学习总结(80)——统计数据库的总记录数和库中各个表的数据量
查看>>
Mysql学习总结(81)——为什么MySQL不推荐使用uuid或者雪花id作为主键?
查看>>
Mysql学习总结(82)——MySQL逻辑删除与数据库唯一性约束如何解决?
查看>>
Mysql学习总结(83)——常用的几种分布式锁:ZK分布式锁、Redis分布式锁、数据库分布式锁、基于JDK的分布式锁方案对比总结
查看>>
Mysql学习总结(84)—— Mysql的主从复制延迟问题总结
查看>>
Mysql学习总结(85)——开发人员最应该明白的数据库设计原则
查看>>
Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解
查看>>
Mysql学习总结(9)——MySql视图原理讲解与使用大全
查看>>
Mysql学习笔记 - 在Centos7环境下离线安装Mysql
查看>>
MySQL学习笔记十七:复制特性
查看>>