一个比较好理解的方法,先在这里留个坑,周末补充一下内容

反转链表

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

/*
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 == nullptr) {
		  return nullptr;
	   }
	   ListNode *cur = pHead;
	   ListNode *pre = nullptr;
	   while (cur != nullptr) {
		ListNode *tmp = cur->next;
		cur->next = pre;
		pre = cur;
		cur = tmp;

	   }
	   return pre;
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务