typedef struct node
{
int data;
struct node* pNext;
}Node;
Node * SortLinkList (Node * pHead);
Node* SortLinkList(Node *pHead)
{
/* 1, defines and initialize */
Node *q = NULL;
Node *p = NULL;
Node *i = NULL;
Node *j = NULL;
if (NULL == pHead)
{
return NULL;
}
if (NULL == pHead->pNext)
{
return pHead;
}
/* 2, iterate and insert */
j = pHead->pNext;
i = pHead->pNext->pNext;
while (NULL != i)
{
q = pHead;
p = q->pNext;
while (p != i && i->data >= p->data)
{
q = p;
p = p->pNext;
}
if (p != i)
{
j->pNext = i->pNext;
q->pNext = i;
i->pNext = p;
i = j->pNext;
}
else
{
j = i;
i = i->pNext;
}
}
/* 3, return */
return pHead;
}