题解 | #反转链表#

反转链表

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

js

    this.val = x;
    this.next = null;
}*/
function ReverseList(pHead)
{
    // write code here
    if(!pHead || !pHead.next){
        return pHead
    }
    let pre = null
    let cur = null
    while(pHead){
        cur = pHead.next
        pHead.next = pre
        pre = pHead
        pHead = cur
    }
    return pre
}
module.exports = {
    ReverseList : ReverseList
};

alt

C语言

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

/**
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */
struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    if(pHead == NULL)
        return NULL;
    struct ListNode*temp = NULL;
    struct ListNode*pre = NULL;
    while(pHead != NULL){
        temp = pHead->next;
        pHead -> next = pre;
        pre = pHead;
        pHead = temp;
   }
    return pre;
}

alt

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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