博客
关于我
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启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>