利用指针p依次指向㊣向的链表 q1与q依次将正向链表的next指向前一位,q作为改变指向之后遍历的指针
/*struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* C语言声明定义全局变量请加上static,防止重复定义
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode *p=NULL;
struct ListNode *q=pHead;
struct ListNode *q1=pHead->next;
if(pHead==NULL)
return NULL;
if(pHead->next==NULL)
return pHead;
q->next=p;
while(q1!=NULL){
p=q1->next;
q1->next=q;
q=q1;
q1=p;
}
return q;
};