206. 反转链表

class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return nullptr;
ListNode* cur = head->next;
if(!cur) return head;
head->next = nullptr;
while(cur){
ListNode* temp = cur->next;
cur->next = head;
head = cur;
cur = temp;
}
return head;
}
};

浙公网安备 33010602011771号