题解 | #反转链表#

反转链表

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;
    
}


全部评论

相关推荐

ResourceUtilization:四六级不愧是大学最有用的证之一
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务