Created
December 25, 2018 19:25
-
-
Save kanrourou/add3446510380bb71c063a2196156712 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Definition for singly-linked list. | |
| * struct ListNode { | |
| * int val; | |
| * ListNode *next; | |
| * ListNode(int x) : val(x), next(NULL) {} | |
| * }; | |
| */ | |
| class Solution { | |
| public: | |
| ListNode* reverseList(ListNode* head) { | |
| return reverse(head); | |
| } | |
| private: | |
| ListNode* reverse(ListNode* head) | |
| { | |
| if(!head || !head->next)return head; | |
| ListNode* tail = head->next; | |
| ListNode* newHead = reverse(head->next); | |
| tail->next = head; | |
| head->next = nullptr; | |
| return newHead; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment