/**
 * 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
    // 就地尾插法
    struct ListNode *p=pHead,*q=pHead->next;
    struct ListNode *first = (struct ListNode*)malloc(sizeof(struct ListNode));
    first->val = NULL;                //创造头结点
    first->next = pHead;
    if(pHead == NULL || pHead->next == NULL)
        return pHead;
    while(p->next!=NULL){
        p->next = q->next;
        q->next = first->next;
        first->next = q;
        if(p->next!=NULL)
            q = p->next;           
    }
    free(first);
    return q;
}