题解 | #反转链表#

反转链表

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

struct ListNode* ReverseList(struct ListNode* pHead ) {

    // write code here

    struct ListNode* tmp = ( struct ListNode*)malloc(sizeof(struct ListNode));

    tmp->next = NULL;

//空列表头节点

    struct ListNode* n = ( struct ListNode*)malloc(sizeof(struct ListNode));

//n来暂存下一个,当pHead是空的时候就不插入

    for (  n = pHead->next; pHead != NULL; n = pHead->next) {

        pHead->next = tmp->next;

        tmp->next = pHead;

        pHead = n;

    }

    return tmp->next;

}

全部评论

相关推荐

投递美团等公司9个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务