zoukankan      html  css  js  c++  java
  • LeetCode:Sort List

    Title:

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

    思路:考虑快速排序和归并排序,但是我的快速排序超时了

    ListNode* sortList(ListNode* head) {
            if (!head)
                return NULL;
            int m_val = head->val;
            ListNode* left = NULL,*right = NULL;
            ListNode* p_left = NULL,*p_right = NULL;
            ListNode* p = head->next;
            if (p == NULL)
                return head;
            while (p){
                if (p->val < m_val){
                    if (left == NULL)
                        left = p;
                    else
                        p_left->next = p;
                    p_left = p;
                }else{
                    if (right == NULL)
                        right = p;
                    else
                        p_right->next = p;
                    p_right = p;
                }
                p = p->next;
            }
            if (p_left)
                p_left->next = NULL;
            if (p_right)
                p_right->next = NULL;
            ListNode* sorted_left = sortList(left);
            ListNode* sorted_right = sortList(right);
            ListNode *sorted = new ListNode(0);
            p = sorted_left;
            ListNode* p_cur = sorted;
            while (p){
                p_cur->next = p;
                p_cur = p;
                p = p->next;
            }
            p_cur->next = head;
            p_cur = head;
            p_cur->next = sorted_right;
            return sorted->next;
        }

    归并排序。两种思路,一个得到全部长度之后,进行。另一种使用快慢指针

    class Solution {
    private:
        int len(ListNode* head){
            int l = 0;
            ListNode*p = head;
            while (p){
                l++;
                p = p->next;
            }
            return l;
        }
    
    public:
        ListNode* sortList(ListNode* head){
            if (head == NULL)
                return NULL;
            int length = len(head);
            
            return sort(head,length);
        }
        ListNode* sort(ListNode*& head,int length){
            if (length == 1){
                ListNode* t = head;
                head = head->next;//这个地方要注意。同时使用指针引用的目的是让head一直往后。因为总是左边的先走,所以到sort(head,length-length/2)时head刚好就是那个位置。
                t->next =  NULL;
                return t;
            }
            ListNode*left = sort(head,length/2);
            ListNode*right = sort(head,length-length/2);
            return merge(left,right);
        }
        ListNode* merge(ListNode* left,ListNode* right){
            ListNode* head = new ListNode(0);
            ListNode* p = head;
            while (left && right){
                if (left->val < right->val){
                    p->next = left;
                    p = left;
                    left = left->next;
                }else{
                    p->next = right;
                    p = right;
                    right = right->next;
                }
            }
            while (left){
                p->next = left;
                p = left;
                left = left->next;
            }
            while (right){
                p->next = right;
                p = right;
                right = right->next;
            }
            return head->next;
            
        }
    }
    class Solution {
    public:
        ListNode *sortList(ListNode *head) {
            if(!head||!head->next)
                return head;
            return mergeSort(head);
        }
        ListNode * mergeSort(ListNode *head){
            if(!head||!head->next)   //just one element
                return head;
            ListNode *p=head, *q=head, *pre=NULL;
            while(q&&q->next!=NULL){
                q=q->next->next;
                pre=p;
                p=p->next;  //divide into two parts
            }
            pre->next=NULL;
            ListNode *lhalf=mergeSort(head);
            ListNode *rhalf=mergeSort(p);  //recursive
            return merge(lhalf, rhalf);   //merge
        }
        ListNode * merge(ListNode *lh, ListNode *rh){
            ListNode *temp=new ListNode(0);
            ListNode *p=temp;
            while(lh&&rh){
                if(lh->val<=rh->val){
                    p->next=lh;
                    lh=lh->next;
                }
                else{
                    p->next=rh;
                    rh=rh->next;
                }
                p=p->next;
            }
            if(!lh)
                p->next=rh;
            else
                p->next=lh;
            p=temp->next;
            temp->next=NULL;
            delete temp;
            return p;
        }
    };
  • 相关阅读:
    门头沟PHP,java编程兴趣小组欢迎大家的加入。
    mysql数据库怎么存入emoji表情,更改utf8mb4后为什么出现全是问号
    Laravel发送邮件配置-问题解决
    php版本低更换php版本-问题以解决
    极光客户互动云java post请求
    【问题解决】增加https后 phpcms 分页错误
    阿里云阿里免费ssl wap网站在手机微信、手机浏览器无法访问
    Kali Linux on Android # 实测:小米2s离线安装Kali Linux
    3-1 LVS-NAT集群
    使用LVM方式安装Ubuntu 16.04
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4527844.html
Copyright © 2011-2022 走看看