题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

/**
 * 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->next==NULL)
        return pHead;
    struct ListNode* list=NULL;
    struct ListNode* list2=NULL;
    while(1)
    {
        list2=pHead->next;
        pHead->next=list;
        list=pHead;
        pHead=list2;
        if(pHead->next==NULL)
        {
            pHead->next=list;
            break;
        }
    }
    return pHead;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务