【sort-list】

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

题意实现一个链表排序的算法;


方法一:刚开始想的是自己定义个比较函数,然后调用sort()函数进行实现

class Solution
{
public:

	static bool cmp(ListNode* a, ListNode* b)
	{
		return a->val < b->val;
	}

	ListNode* sortList(ListNode* head)
	{
		if (head==NULL)
		{
			return head;
		}

		vector<ListNode*> arrList;
		ListNode* p = head;

		while (p)
		{
			arrList.push_back(p);
			p = p->next;
		}

		sort(arrList.begin(), arrList.end());

		p = head = arrList[0];

		for (int i=1; i<arrList.size(); i++)
		{
			p->next = arrList[i];
			p = p->next;
		}

		arrList[arrList.size() - 1]->next = NULL;

		return head;

	}
};


方法二:因为题目要求的时间负责度O(nlogn), 故可以考虑归并排序的思想;

归并排序的一把步骤:

(1)将待排序数组去中间点并一分为二;

(2)递归地对左半部分进行归并排序;

(3)递归地对右半部分进行归并排序;

(4)将两个半部分进行合并,得到结果;

所以对应此题,可以分为三个小问题;

(1)找到链表中点;

(2)写出merge函数,即如何合并链表

(3)写出mergesort函数,实现上述步骤;

class Solution
{
public:

	//找到链表中间位置
	ListNode* findMid(ListNode* head)
	{
		ListNode* slow = head;
		ListNode* fast = head;

		while (fast!=NULL && fast->next!=NULL && fast->next->next!=NULL)
		{
			slow = slow->next;
			fast = fast->next->next;
		}

		return slow;
	}

	//合并两个有序链表
	ListNode* mergeList(ListNode* h1, ListNode* h2)
	{
		if (h1 == NULL)
		{
			return h2;
		}
		else if (h2 == NULL)
		{
			return h1;
		}
		else
		{
			ListNode* head = NULL;
			if (h1->val>h2->val)
			{
				head = h2;
				h2 = h2->next;
			}
			else
			{
				head = h1;
				h1 = h1->next;
			}

			ListNode* p = head;
			while (h1 && h2)
			{
				if (h1->val < h2->val)
				{
					p->next = h1;
					h1 = h1->next;
				}
				else
				{
					p->next = h2;
					h2 = h2->next;
				}
				p = p->next;
			}

			if (h1) p->next = h1;
			if (h2) p->next = h2;

			return head;
		}
	}
	ListNode* sortList(ListNode* head)
	{
		if (head == NULL || head->next == NULL)
		{
			return head;
		}

		ListNode* mid = findMid(head);

		ListNode* h2 = sortList(mid->next);
		mid->next = NULL;
		ListNode* h1 = sortList(head);

		return mergeList(h1, h2);
	}
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值