链表反转

输入一个链表,反转链表后,输出新链表的表头

/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
ListNode* ReverseList(ListNode* pHead)
{
	if(pHead==NULL || pHead->next==NULL)
		return pHead;
	ListNode* tailHead;
	ListNode* temp;
	ListNode* tempNext;
	tailHead=pHead;
	temp=pHead->next;
	tempNext=pHead->next;
	pHead->next=NULL;
	while(tempNext->next!=NULL)
	{
		tempNext=temp->next;
		temp->next=tailHead;
		tailHead=temp;
		temp=tempNext;
	}
	temp->next=tailHead;
	tailHead=temp;
	return tailHead;
}

  

  

原文地址:https://www.cnblogs.com/dongdong25800/p/9852676.html