Created
March 25, 2019 09:11
-
-
Save sonsongithub/973d41d95d1ef454bd621e85c1e3147c to your computer and use it in GitHub Desktop.
Fibonacci Number
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
| #include <vector> | |
| #include <iostream> | |
| using namespace std; | |
| // 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 (head == NULL) ? NULL : doit(head); | |
| } | |
| ListNode* doit(ListNode* node) { | |
| if (node->next != NULL) { | |
| ListNode* head = doit(node->next); | |
| ListNode* newNode = new ListNode(node->val); | |
| ListNode* iterator = head; | |
| while(iterator->next != NULL) { | |
| iterator = iterator->next; | |
| } | |
| iterator->next = newNode; | |
| return head; | |
| } else { | |
| return new ListNode(node->val); | |
| } | |
| } | |
| }; | |
| int main(int argc, char**argv) { | |
| auto a = ListNode(1); | |
| auto b = ListNode(3); | |
| auto c = ListNode(4); | |
| auto d = ListNode(2); | |
| a.next = &b; | |
| b.next = &c; | |
| c.next = &d; | |
| auto obj = Solution(); | |
| ListNode* p = obj.reverseList(&a); | |
| ListNode* iterator = p; | |
| while(iterator != NULL) { | |
| cout << iterator->val << endl; | |
| iterator = iterator->next; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment