题解 | #NC78-反转链表#

反转链表

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

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */

struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    if(pHead == NULL || pHead->next == NULL)
        return pHead;
    
    struct ListNode *p = NULL;
    struct ListNode *tmp;
    
    while(pHead != NULL)
    {
        tmp = pHead->next;//记录当前节点的下一个节点
        pHead->next = p;//当前节点的next指向反转后的链表
        p = pHead;//P指向反转后的第1个节点
        pHead = tmp;//继续操作下一个节点
    }
    
    return p;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务