链表逆置

int SList_revse(SLIST * pHead)
{
	SLIST *p = NULL, *q = NULL, *t = NULL;
	if (pHead == NULL)
	{
		return -1;
	}
	if (pHead->next == NULL || pHead->next->next == NULL)
	{
		return 0;
	}

	//准备环境
	p = pHead->next;
	q = pHead->next->next;

	while (q != NULL)
	{
		t = q->next; //逆置之前需要做一个缓存

		q->next = p; //逆置

		p = q;

		q = t;
	}
	pHead->next ->next = NULL;
	pHead->next = p;

	return 0;
}

链表逆置_第1张图片 




你可能感兴趣的:(链表逆置)