链表——翻转链表

struct ListNode 
{
	int val;
	ListNode* next;
	ListNode(int val) :val(val), next(NULL) {};
};

class Solution
{
public:
	ListNode* reverseList(ListNode* head)
	{
		ListNode* temp;			// 保存cur的下一个节点
		ListNode* cur = head;	// 初始化操作
		ListNode* pre = NULL;

		while (cur)
		{
			temp = cur->next;
			cur->next = pre;
			pre = cur;
			cur = temp;
		}
		return pre;
	}
};
posted @ 2022-10-26 16:55  香花草的味道  阅读(29)  评论(0)    收藏  举报