#include <iostream>
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {}
};
void print(Node *head);
Node * reverse(Node * start, Node * end, Node * left, Node * right);
Node * reverseKNode(Node * head, int k);
int main()
{
Node n1(1);
Node n2(2);
Node n3(3);
Node n4(4);
Node n5(5);
Node n6(6);
Node n7(7);
Node n8(8);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
print(reverseKNode(&n1, 4));
system("pause");
return 0;
}
void print(Node *head) //打印函数
{
while (head != nullptr)
{
std::cout << head->value << std::endl;
head = head->next;
}
}
Node * reverseKNode(Node * head, int k)
{
if (head == nullptr || k < 2) {
return head;
}
Node * newHead = head;
Node *cur = head; //cur表示当前指向的Node
Node *right = nullptr; //反转k个元素的下一个元素
Node *left = nullptr; //反转K个元素的前一个元素,若没有则为nullptr
Node *start = head; //将要反转的第一个元素
int count = 0;
while (cur != nullptr) {
++count;
right = cur->next;
if (count % k == 0 && cur != nullptr) {
left = reverse(start, cur, left, right);
start = left->next;
newHead = newHead == head ? cur : newHead;
count = 0;
cur = left; //reverser()之后 将cur重置 将其设置为reverse的第一个Node
}
cur = cur->next;
}
return newHead;
}
Node * reverse(Node * start, Node * end, Node * left, Node * right) //反转部分链表
{
Node * pre = start;
Node * cur = start->next;
Node * next = nullptr;
while (cur != right) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
if (left != nullptr) {
left->next = end;
}
start->next = right;
return start;
}
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {}
};
void print(Node *head);
Node * reverse(Node * start, Node * end, Node * left, Node * right);
Node * reverseKNode(Node * head, int k);
int main()
{
Node n1(1);
Node n2(2);
Node n3(3);
Node n4(4);
Node n5(5);
Node n6(6);
Node n7(7);
Node n8(8);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
print(reverseKNode(&n1, 4));
system("pause");
return 0;
}
void print(Node *head) //打印函数
{
while (head != nullptr)
{
std::cout << head->value << std::endl;
head = head->next;
}
}
Node * reverseKNode(Node * head, int k)
{
if (head == nullptr || k < 2) {
return head;
}
Node * newHead = head;
Node *cur = head; //cur表示当前指向的Node
Node *right = nullptr; //反转k个元素的下一个元素
Node *left = nullptr; //反转K个元素的前一个元素,若没有则为nullptr
Node *start = head; //将要反转的第一个元素
int count = 0;
while (cur != nullptr) {
++count;
right = cur->next;
if (count % k == 0 && cur != nullptr) {
left = reverse(start, cur, left, right);
start = left->next;
newHead = newHead == head ? cur : newHead;
count = 0;
cur = left; //reverser()之后 将cur重置 将其设置为reverse的第一个Node
}
cur = cur->next;
}
return newHead;
}
Node * reverse(Node * start, Node * end, Node * left, Node * right) //反转部分链表
{
Node * pre = start;
Node * cur = start->next;
Node * next = nullptr;
while (cur != right) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
if (left != nullptr) {
left->next = end;
}
start->next = right;
return start;
}