Files
LeetCode/Easy/reverse-linked-list/solution.cpp
Arkaprabha Chakraborty cab1180397 Refactor almost everything
2022-08-02 12:21:43 +05:30

23 lines
525 B
C++

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 *prev = nullptr, *next = nullptr, *curr = head;
while (curr != nullptr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};