diff --git a/reverse-linked-list/crumbs22.cpp b/reverse-linked-list/crumbs22.cpp new file mode 100644 index 000000000..20ca0893e --- /dev/null +++ b/reverse-linked-list/crumbs22.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +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* prv = nullptr; + while (cur) { + ListNode* rNode = cur->next; + cur->next = prv; + prv = cur; + cur = rNode; + } + return (prv); + } +};