题目描述
我的解题
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==nullptr || head->next==nullptr) return head;
ListNode *p=head, *q=p->next, *r=nullptr;
p->next = nullptr;
while(q)
{
r = q->next;
q->next = p;
p=q;
q=r;
}
return p;
}
};
执行用时 :8 ms, 在所有 C++ 提交中击败了78.52%的用户
内存消耗 :10.3 MB, 在所有 C++ 提交中击败了5.04%的用户