zoukankan      html  css  js  c++  java
  • Sort List

    归并排序的链表法

    #include<iostream>
    using namespace std;
    
    struct ListNode
    {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    };
    
    class Solution
    {
    public:
        ListNode* mergeLists(ListNode *a,ListNode *b)
        {
            if(a==NULL) return b;
            if(b==NULL) return a;
            ListNode *ret;
            ret = new ListNode(-1);
            ListNode *tail = ret;
            while(a && b)
            {
                if(a->val<b->val)
                {
                    tail->next=a;
                    a=a->next;
                }
                else
                {
                    tail->next=b;
                    b=b->next;
                }
                tail=tail->next;
            }
            if(a) tail->next=a;
            if(b) tail->next=b;
            ListNode *del;
            del=ret;
            ret=ret->next;
            delete del;
            return ret;
        }
        ListNode *getMid(ListNode *head)
        {
            if(!head) return NULL;
            if(!head->next) return head;
    
            ListNode *slow = head;
            ListNode *fast = head->next;
    
            while(fast && fast->next)
            {
                slow = slow->next;
                fast = fast->next->next;
            }
            return slow;
        }
        ListNode *sortList(ListNode *head)
        {
            if(!head) return NULL;
            if(!head->next) return head;
    
            ListNode *mid = getMid(head);
            ListNode *nextPart = NULL;
            if(mid)
            {
                nextPart = mid->next;
                mid->next = NULL;
            }
    
            return mergeLists(
                       sortList(head),
                       sortList(nextPart));
        }
    } t;
    
    /*ListNode* ListNodeCreate()
    {
        int data;
        ListNode *ret;
        ret = new ListNode(-1);
        ListNode *tail;
        tail=ret;
        while(cin>>data)
        {
            if(data==0) break;
            ListNode *p= new ListNode(data);
            tail->next = p;
            tail =tail->next;
        }
        ListNode *del;
        del=ret;
        ret=ret->next;
        delete del;
        return ret;
    }
    
    void PrintList(ListNode *L)
    {
        while(L)
        {
            cout<<L->val<<" ";
            L=L->next;
        }
    }
    int main()
    {
        ListNode *L;
        L=ListNodeCreate();
        ListNode *T=t.sortList(L);
        PrintList(T);
        return 0;
    }*/
    

      

  • 相关阅读:
    053573
    053572
    053571
    053570
    053569
    053568
    Android:你好,androidX!再见,android.support
    最新Androidx Fragment的前世今生系列(一)之Fragment的简单使用
    Android开发中如何匹配layout资源(layoutsw480dp layoutsw600dpland layoutsw720dpport)
    Android:Fragment最全面介绍 & 使用方法解析
  • 原文地址:https://www.cnblogs.com/zsboy/p/3877079.html
Copyright © 2011-2022 走看看