132-对链表分隔进行排序

head=1->4->3->2->5->2 val-3

输出 1->2->2->4->3->5

int *StakeHeadlist(HeadList *head,int val)
{
    
    
	if(head==NULL) exit(0);

	HeadList* p = head->next;
	HeadList* lefthead = NULL;//左链表头 
	HeadList* leftail =  NULL;//左链表尾 
	HeadList* righthead = NULL;//右链表头 
	HeadList* righttail = NULL;//右链表尾 
	while (p != NULL)//从头结点开始遍历 
	{
    
    
		if (p->val >= val)
		{
    
    
			if (righthead == NULL) 
			{
    
    
				righthead = p;
				righttail = p;
			} 
			else 
			{
    
    
				righttail->next = p;
				righttail = righttail->next;
			}
		} 
		else 
		{
    
    
			if (lefthead == NULL) 
			{
    
    
				lefthead = p;
				lefttail = p;
			} 
			else 
			{
    
    
				lefttail->next = p;
				lefttail = lefttail->next;
			}
		}
		p = p->next;
	}

	if (righthead !=NULL) 
	{
    
    
		righttail->next =NULL;
	}
	if (lefthead == NULL) 
	{
    
    
		return righthead;
	} 
	else 
	{
    
    
		lefttail->next = righthead;
		return lefthead;
	}
}

猜你喜欢

转载自blog.csdn.net/LINZEYU666/article/details/112337778