题目地址: https://leetcode-cn.com/problems/reverse-linked-list/
题目描述: 反转一个单链表
参考代码:
struct ListNode {
int val;
ListNode *next;
ListNode():val(0),next(nullptr){}
ListNode(int x):val(x),next(nullptr){}
ListNode(int x ,ListNode *next):val(x),next(next){}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur = head;
ListNode *pre = nullptr;
ListNode *temp = nullptr;
while (cur) {
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
网友评论