# sort-list

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

## 思路一：

#include <iostream>
#include "../coding interviews/Utilities/List.h"
using namespace std;
ListNode* pNext = nullptr;//内循环指针
ListNode* pPNext = nullptr;//永远跟在pNext的前一个
ListNode* temp = nullptr;
ListNode* sortListCore(ListNode *pbegin, ListNode* pend, ListNode* pPreBegin, ListNode* pHead)
{
if(pbegin == pend)
return pbegin;

pNext = pbegin->m_pNext;//内循环指针
pPNext = pbegin;//永远跟在pNext的前一个
temp = nullptr;
while(pNext != pend)
{
if(pbegin->m_nValue > pNext->m_nValue)
{
if(pPreBegin == nullptr)
{
temp = pNext->m_pNext;
pNext->m_pNext = pbegin;
pPNext->m_pNext = temp;
pPreBegin    = pNext;
pNext       = pPNext->m_pNext;
{
}
}
else
{
temp = pNext->m_pNext;
pNext->m_pNext    = pbegin;
pPNext->m_pNext    = temp;
pPreBegin->m_pNext = pNext;
pPreBegin       = pNext;
pNext          = pPNext->m_pNext;
}
}
else
{
pPNext = pNext;
pNext = pNext->m_pNext;
}
}

}

{
//            return nullptr;
//ListNode* pPreNode = nullptr;//永远跟在pNode的前一个

}

int main()
{
ListNode* pNode1 = CreateListNode(5);
ListNode* pNode2 = CreateListNode(4);
ListNode* pNode3 = CreateListNode(6);
ListNode* pNode4 = CreateListNode(2);
ListNode* pNode5 = CreateListNode(1);
ListNode* pNode6 = CreateListNode(0);

ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
ConnectListNodes(pNode3, pNode4);
ConnectListNodes(pNode4, pNode5);
ConnectListNodes(pNode5, pNode6);

PrintList(pNode1);

ListNode* newNode = sortList(pNode1);

PrintList(newNode);
}

## 思路二


//  struct ListNode {
//      int val;
//      ListNode *next;
//      ListNode(int x) : val(x), next(nullptr) {}
//  };
#include <iostream>
#include "../coding interviews/Utilities/List.h"
using namespace std;
ListNode* pRNext = nullptr;//要归并的后一个数组的头指针
ListNode* pLNext  = nullptr;//要归并的前一个数组的头指针

ListNode* temp = nullptr;
ListNode* pmid = nullptr;//标示中间节点的位置
ListNode* Getmid(ListNode *pbegin, ListNode* pend)
{
temp = pbegin;
pmid  = pbegin;
if(temp == pend)
{
return temp;
}
else
{
temp = temp->next;
if(temp == pend)
return pmid;
else
temp = temp->next;
}

while(temp != pend && temp->next != pend)
{
pmid = pmid->next;
temp = temp->next->next;
}
return pmid;
}

ListNode* sortListCore(ListNode *pbegin, ListNode* pend, ListNode* pmiddle)
{
if(pbegin != pmiddle->next && pmiddle->next->next != pend && pmiddle->next != pend)
{
pmid = Getmid(pmiddle->next,  pend);
pmiddle->next = sortListCore(pmiddle->next, pend, pmid);
//PrintList(pmiddle->next);
pmid = Getmid(pbegin,  pmiddle->next);
pbegin = sortListCore(pbegin, pmiddle->next, pmid);
}

pLNext = pbegin;
pRNext = pmiddle->next;
pmiddle->next = nullptr;//将链表拆开
ListNode *dumy = CreateListNode(0);
temp = dumy;

while(pLNext && pRNext)
{
if(pLNext->val > pRNext->val)
{
temp->next = pRNext;
pRNext     = pRNext->next;
temp = temp->next;
}
else
{
temp->next = pLNext;
pLNext     = pLNext->next;
temp = temp->next;
}
}
if(pLNext)   temp->next = pLNext;
if(pRNext)   temp->next = pRNext;
temp = dumy->next;
delete dumy;
dumy = nullptr;
return temp;
}

{
return nullptr;
// PrintListNode(pmid);
}

int main()
{
ListNode* pNode1 = CreateListNode(3);
ListNode* pNode2 = CreateListNode(4);
ListNode* pNode3 = CreateListNode(1);
//    ListNode* pNode4 = CreateListNode(0);
//    ListNode* pNode5 = CreateListNode(1);
//    ListNode* pNode6 = CreateListNode(4);

ConnectListNodes(pNode1, pNode2);
ConnectListNodes(pNode2, pNode3);
//    ConnectListNodes(pNode3, pNode4);
//    ConnectListNodes(pNode4, pNode5);
//    ConnectListNodes(pNode5, pNode6);

PrintList(pNode1);

ListNode* newNode = sortList(pNode1);

PrintList(newNode);

DestroyList(newNode);
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120