Created
August 12, 2022 04:50
-
-
Save ijingo/2d9483bc8d3d90971d13ec823b9919d1 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <vector> | |
struct ListNode { | |
int val; | |
ListNode* next; | |
ListNode(): ListNode(0, nullptr) {} | |
ListNode(int x): ListNode(x, nullptr) {} | |
ListNode(int x, ListNode* next): val(x), next(next) {} | |
}; | |
void Traverse(ListNode* head) { | |
std::cout << "["; | |
if (head != nullptr) { | |
std::cout << head->val; | |
head = head->next; | |
} | |
while (head != nullptr) { | |
std::cout << ", " << head->val; | |
head = head->next; | |
} | |
std::cout << "]" << std::endl; | |
} | |
ListNode* GenerateListFromVector(const std::vector<int>& arr) { | |
ListNode dummy; | |
ListNode* prev = &dummy; | |
for (auto& val : arr) { | |
prev->next = new ListNode(val); | |
prev = prev->next; | |
} | |
return dummy.next; | |
} | |
ListNode* ReverseInterval(ListNode* head, int left, int right) { | |
return head; | |
} | |
int main() { | |
// case 1: | |
// input: | |
// [1, 2, 3, 4, 5] | |
// 1 | |
// 5 | |
// output: | |
// [5, 4, 3, 2, 1] | |
// | |
auto* head1 = GenerateListFromVector({1, 2, 3, 4, 5}); | |
head1 = ReverseInterval(head1, 1, 5); | |
Traverse(head1); | |
// case 2: | |
// input: | |
// [1, 2, 3, 4, 5] | |
// 1 | |
// 2 | |
// output: | |
// [2, 1, 3, 4, 5] | |
// | |
// | |
auto* head2 = GenerateListFromVector({1, 2, 3, 4, 5}); | |
head2 = ReverseInterval(head2, 1, 2); | |
Traverse(head2); | |
// case 3: | |
// input: | |
// [1, 2, 3, 4, 5] | |
// 2 | |
// 4 | |
// output: | |
// [1, 4, 3, 2, 5] | |
auto* head3 = GenerateListFromVector({1, 2, 3, 4, 5}); | |
head3 = ReverseInterval(head3, 2, 4); | |
Traverse(head3); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment