list链表构建和归并排序

#include<iostream>
#include<vector>

using namespace std;
struct ListNode {
   int m_nValue;
   ListNode *m_pNext;
   ListNode(int x) : m_nValue(x), m_pNext(NULL) {}
};

void AddToTail(ListNode ** pHead, int value)
{
        ListNode * pNew = new ListNode(0);
        pNew->m_nValue = value;
        pNew->m_pNext = NULL;
        if(*pHead == NULL)
        {
                *pHead = pNew;
        }else
        {
                ListNode *pNode = *pHead;
                while(pNode->m_pNext!=NULL)
                {
                        pNode = pNode->m_pNext;
                }
                pNode->m_pNext = pNew;
        }
}

void ConstructList(ListNode **pHead, vector<int> &list_val)
{
        int size = list_val.size();
        for(int i=0;i<size; i++)
        {

                cout<<"input:"<<list_val[i]<<endl;
                AddToTail(pHead, list_val[i]);
        }
}

int GetListSize(ListNode *head)
{
    int i ;
    for(i = 0 ; head ; i++)
         head = head->m_pNext;
    return i;
}

ListNode * Access_K(ListNode* head,int k){

        for(int i=0; i<k; i++)
        {
                head = head->m_pNext;
        }
        return head;
}

ListNode * Merge(ListNode *list1, ListNode *list2)
{
        if(list1==NULL) return list2;
        if(list2==NULL) return list1;
        if(list1==NULL&&list2==NULL) return NULL;
        ListNode *ret;
        ret = list1->m_nValue <= list2->m_nValue ? list1 : list2;
        ret->m_pNext = list1->m_nValue <= list2->m_nValue ? Merge(list1->m_pNext, list2) : Merge(list1, list2->m_pNext);
        return ret;
}

void PrintList(ListNode *pHead)
{
    ListNode *pNode = pHead;
    cout<<"================="<<endl;
    while(pNode!=NULL)
    {   
        cout<<pNode->m_nValue<<endl;
        pNode = pNode->m_pNext;                                                                                                     
    }   
}

ListNode * SortList(ListNode *pHead)
{
        ListNode *pNode = pHead;
        int length = GetListSize(pNode);
        if(length<=1) return pNode;
        ListNode * ptr = Access_K(pNode, length/2-1);
        ListNode * list1 = SortList(ptr->m_pNext);
        PrintList(list1);
        ptr->m_pNext = NULL;
        ListNode * list2 = SortList(pNode);
        PrintList(list2);
        return Merge(list1, list2);
}

int main(int argc, char *argv[])
{
        ListNode **pHead = new ListNode*[1];
        *pHead = NULL;
        vector<int> list_val ;
        list_val.push_back(1);
        list_val.push_back(2);
        list_val.push_back(5);
        list_val.push_back(2);
        list_val.push_back(3);
        list_val.push_back(1);
        list_val.push_back(6);
        list_val.push_back(9);
        list_val.push_back(0);
        ConstructList(pHead, list_val);
        PrintList(*pHead);
        ListNode *result = SortList(*pHead);
        PrintList(result);
}

阅读更多
个人分类: 算法
上一篇150. Evaluate Reverse Polish Notation
下一篇60. Permutation Sequence
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭