标签:
给链表的结点进行排序。比如给出 1->3->2->0->null ,排序后 0->1->2->3->null。 这里介绍链表的插入排序和归并排序。
插入排序就是已经前面
最主要的思想是两个指针,一个指针指向已经排序好的链表,另外一个指向未排序的链表。每次比较,都要从头遍历已排序好的链表。
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The head of linked list.
*/
ListNode *insertionSortList(ListNode *head) {
// write your code here
if(NULL == head)
return head;
ListNode *pNext, *prev, *curNode, *q;
//head指向已经排序好的链表,pNext指向未排序的链表。实际是都是同一个链表
pNext = head->next;
head->next = NULL;
while(pNext != NULL)
{
curNode = pNext;
pNext = pNext->next;
int val = curNode->val;
//关键是这个for循环。如果当前未排序的结点大于已排序好的,继续往下比较,并保存已排序好的结点,方便后序插入。如果小于,则退出for循环
for(prev = NULL, q = head; q && q->val <= val; prev = q, q = q->next );
//比头节点小
if(q == head)
{
head = curNode;
}
else
{
prev->next = curNode;
}
//插入操作
curNode->next = q;
}
return head;
}
};
在数组的归并排序中,归并排序需要
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: You should return the head of the sorted linked list,
using constant space complexity.
*/
ListNode *sortList(ListNode *head) {
// write your code here
if(NULL == head)
return NULL;
int ListLen = 0;
ListNode *pNode = head;
while(pNode != NULL)
{
ListLen ++;
pNode = pNode->next;
}
return sortList_core(head, ListLen);
}
private:
ListNode *sortList_core(ListNode *head, int ListLen)
{
if(NULL == head || ListLen <= 0)
return head;
int mid = ListLen / 2;
ListNode *midList = head;
int count = 1;
while(count < mid)
{
midList = midList->next;
count ++;
}
ListNode *rList = sortList_core(midList->next, ListLen - mid);
midList->next = NULL;
ListNode *lList = sortList_core(head, mid);
return Merge2List(lList, rList);
}
ListNode *Merge2List(ListNode *L1, ListNode *L2)
{
ListNode *dummy = new ListNode(0);
ListNode *lastNode = dummy;
while((L1 != NULL) && (L2 != NULL))
{
if(L1->val < L2->val)
{
lastNode->next = L1;
L1 = L1->next;
}
else
{
lastNode->next = L2;
L2 = L2->next;
}
lastNode = lastNode->next;
}
lastNode->next = (NULL != L1)? L1 : L2;
return dummy->next;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zwhlxl/article/details/48010593