题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode *temp1 = NULL,*temp2 = pHead,*temp3 = pHead->next;
if(NULL == pHead || NULL == pHead->next) return pHead;
while(NULL != temp3)
{
temp2->next = temp1;
temp1 = temp2;
temp2 = temp3;
temp3 = temp2->next;
}
temp2->next = temp1;
return temp2;
}