Created
December 22, 2021 15:24
-
-
Save claytonjwong/36ad8d3a6f9e93ba2216807058736672 to your computer and use it in GitHub Desktop.
reverse linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Recursively change each node of the linked list such that next | |
// is the last node seen during a linear scan of the linked list. | |
// | |
class Solution { | |
public: | |
using fun = function<ListNode*(ListNode*, ListNode*)>; | |
ListNode* reverseList(ListNode* head) { | |
fun go = [&](auto node, auto last) { | |
auto next = node->next; | |
node->next = last; | |
if (!next) | |
return node; | |
return go(next, node); | |
}; | |
return head ? go(head, nullptr) : nullptr; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment