#include <iostream>
#include <set>
using namespace std;
struct Node {
int val;
Node* next;
Node(int v, Node* n): val(v), next(n) {}
};
Node* rev(Node* head, int l, int r) {
Node pre(0, head);
Node* first = ⪯
while (l > 1 && first != nullptr) {
first = first->next;
l--;
}
if (first == nullptr) {
return head;
}
cout << "first->val:" << first->val << endl;
auto second = first->next;
int diff = r - l;
while (diff > 0 && second->next != nullptr) {
auto tmp = second->next;
second->next = tmp->next;
tmp = second;
first->next = tmp;
cout << "first :" << first->val << endl;
diff--;
}
return pre.next;
}
int main() {
Node* n5 = new Node(5, nullptr);
Node* n4 = new Node(4, n5);
Node* n3 = new Node(3, n4);
Node* n2 = new Node(2, n3);
Node* n1 = new Node(1, n2);
int left = 2;
int right = 5;
auto res = rev(n1, left, right);
while (res != nullptr) {
cout << res->val << " ";
res = res->next;
}
cout << endl;
return 0;
}