BM1 反转链表
描述
示例1
示例2
思路
题解
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
//当链表为空的情况
if(pHead==NULL)
return NULL;
//当链表不为空的情况
if(pHead!=NULL)
{
struct ListNode *pre = pHead;
struct ListNode *pos = pre-> next;
struct ListNode *tmp = NULL;
while(pos!=NULL)
{
tmp = pos->next;
pos->next = pre;
pre = pos;
pos = tmp;
}
pHead->next=NULL;
pHead = pre;
}
return pHead;
}