题解 | #反转链表#

反转链表

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

原地翻转法 altalt

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

struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    struct ListNode* pre=NULL;
    struct ListNode* cur=pHead;
    struct ListNode* next=cur->next;
    if(!pHead){
        return pHead;
    }
    
    //原地反转法
    while(cur){
        cur->next=pre;
        pre=cur;
        cur=next;
        next=next->next;
    }
    
    return pre;
    
}


全部评论

相关推荐

不愿透露姓名的神秘牛友
01-01 11:31
字节 算法 38x15 +15W 大专
沉淀小子:每次看到这种侮辱价直接拒的装逼仔,就想你是不是秒接了然后在这里装逼,有啥侮辱的。你要真有钱,还需要打工?
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

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