题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=117&tqId=37777&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D117&difficulty=undefined&judgeStatus=undefined&tags=&title=

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
#include <cstddef>
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
		if(pHead == NULL) return NULL;
        ListNode* p = pHead->next;
		ListNode* pre = pHead;
        while(p!=NULL){
           //按照头插法将结点插入

     	   //存储下一个结点
           ListNode *t = p->next;
           p->next = pre;
		   pre = p;
		   p = t;
		}
		pHead->next = NULL;
        return pre;
    }
};

全部评论

相关推荐

牛客245670684号:虚拟货币预测正确率百分之99,还要找工作干嘛,不早就财富自由了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务