数据结构单链表之旋转链表 | 第十五套

``````#include <bits/stdc++.h>
using namespace std;

class Node {
public:
int data;
Node* next;
};

{
if (k == 0)
return;

int count = 1;
while (count < k && current != NULL) {
current = current->next;
count++;
}

if (current == NULL)
return;

Node* kthNode = current;

while (current->next != NULL)
current = current->next;

kthNode->next = NULL;
}

{
Node* new_node = new Node();
new_node->data = new_data;
}

void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}

int main(void)
{

for (int i = 60; i > 0; i -= 10)

cout << "Given linked list \n";

cout << "\nRotated Linked list \n";

return (0);
}

``````Given linked list
10  20  30  40  50  60
50  60  10  20  30  40

``````#include <bits/stdc++.h>
using namespace std;
、
class Node {
public:
int data;
Node* next;
};

{
if (k == 0)
return;
while (current->next != NULL)
current = current->next;

for (int i = 0; i < k - 1; i++)
current = current->next;
current->next = NULL;
}

{
Node* new_node = new Node();

new_node->data = new_data;

}

void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
}

int main(void)
{

for (int i = 60; i > 0; i -= 10)

cout << "Given linked list \n";

cout << "\nRotated Linked list \n";

return (0);
}

``````Given linked list
10 20 30 40 50 60
50 60 10 20 30 40

🥇 往期优质文章

📣尾注： 想要获取更多数据结构相关的知识，你可以关注我：海拥，我希望你觉得这篇文章有帮助。

💌 欢迎大家在评论区提出意见和建议！💌