面试经典题目之一
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode*p=pHead;
if(p==NULL || p->next==NULL)
{
return p;
}
struct ListNode *pre=NULL;
struct ListNode *next=NULL;
while(p!=NULL)
{
next = p->next;
p->next=pre;
pre=p;
p=next;
}
return pre;
}
牛客网在线OJ结果