📜  链表顺时针旋转的C++程序

📅  最后修改于: 2022-05-13 01:54:44.782000             🧑  作者: Mango

链表顺时针旋转的C++程序

给定一个单链表和一个整数K ,任务是将链表顺时针向右旋转K位。
例子:

方法:旋转链表首先检查给定的k是否大于链表中的节点数。遍历链表并找到链表的长度,然后将其与 k 进行比较,如果小于则继续,否则通过对链表的长度取模,在链表大小的范围内推导它。
之后从列表的长度中减去 k 的值。现在,问题已更改为链表的左旋转,因此请按照以下步骤操作:

  • 将第 k 个节点的下一个节点更改为 NULL。
  • 将最后一个节点的下一个节点更改为上一个头节点。
  • 将头部更改为第 (k+1) 个节点。

为此,需要指向第 k 个节点、第 (k+1) 个节点和最后一个节点的指针。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
/* Link list node */
class Node {
public:
    int data;
    Node* next;
};
 
/* A utility function to push a node */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
 
    /* put in the data */
    new_node->data = new_data;
 
    /* link the old list off the new node */
    new_node->next = (*head_ref);
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* A utility function to print linked list */
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << " -> ";
        node = node->next;
    }
    cout << "NULL";
}
 
// Function that rotates the given linked list
// clockwise by k and returns the updated
// head pointer
Node* rightRotate(Node* head, int k)
{
 
    // If the linked list is empty
    if (!head)
        return head;
 
    // len is used to store length of the linked list
    // tmp will point to the last node after this loop
    Node* tmp = head;
    int len = 1;
    while (tmp->next != NULL) {
        tmp = tmp->next;
        len++;
    }
 
    // If k is greater than the size
    // of the linked list
    if (k > len)
        k = k % len;
 
    // Subtract from length to convert
    // it into left rotation
    k = len - k;
 
    // If no rotation needed then
    // return the head node
    if (k == 0 || k == len)
        return head;
 
    // current will either point to
    // kth or NULL after this loop
    Node* current = head;
    int cnt = 1;
    while (cnt < k && current != NULL) {
        current = current->next;
        cnt++;
    }
 
    // If current is NULL then k is equal to the
    // count of nodes in the list
    // Don't change the list in this case
    if (current == NULL)
        return head;
 
    // current points to the kth node
    Node* kthnode = current;
 
    // Change next of last node to previous head
    tmp->next = head;
 
    // Change head to (k+1)th node
    head = kthnode->next;
 
    // Change next of kth node to NULL
    kthnode->next = NULL;
 
    // Return the updated head pointer
    return head;
}
 
// Driver code
int main()
{
 
    /* The constructed linked list is:
    1->2->3->4->5 */
    Node* head = NULL;
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
 
    int k = 2;
 
    // Rotate the linked list
    Node* updated_head = rightRotate(head, k);
 
    // Print the rotated linked list
    printList(updated_head);
 
    return 0;
}


输出:
4 -> 5 -> 1 -> 2 -> 3 -> NULL

时间复杂度: O(n),其中 n 是链表中的节点数。

辅助空间: O(1)

详情请参考链表顺时针旋转的完整文章!