首页 > 编程语言 > 详细

LintCode_173 链表插入排序

时间:2016-05-03 23:54:12      阅读:452      评论:0      收藏:0      [点我收藏+]

题目

用插入排序对链表排序

样例

Given 1->3->2->0->null, return 0->1->2->3->null

C++代码

ListNode *insertionSortList(ListNode *head) {
	// write your code here
	if (!head) return NULL;
	ListNode* root = head;
	head = head->next;
	root->next = NULL;
	ListNode* p;
	while (head)
	{
		p = head;
		head = head->next;
		p->next = NULL;
		ListNode* t, *ft;
		ft = t = root;
		if (root->val >= p->val)
		{
			p->next = root;
			root = p;
		}
		else
		{
			while (t && t->val < p->val)
			{
				ft = t;
				t = t->next;
			}
			if (!t) ft->next = p;
			else
			{
				ft->next = p;
				p->next = t;
			}
		}
	}
	return root;
}

  

LintCode_173 链表插入排序

原文:http://www.cnblogs.com/Smallhui/p/5456876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!