📜  Java程序计算排序和旋转链表中的旋转

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

Java程序计算排序和旋转链表中的旋转

给定一个由 n 个节点组成的链表,该链表首先排序,然后旋转 k 个元素。求 k 的值。

这个想法是遍历单链表来检查当前节点值是否大于下一个节点的值。如果给定条件为真,则中断循环。否则增加计数器变量并通过node->next增加节点。下面是这种方法的实现。

Java
// Program for count number of rotations in
// sorted linked list.
import java.util.*;
  
class GFG
{
    
/* Linked list node */
static class Node {
    int data;
    Node next;
};
  
// Function that count number of
// rotation in singly linked list.
static int countRotation(Node head)
{
    
    // declare count variable and assign it 1.
    int count = 0;
  
    // declare a min variable and assign to
    // data of head node.
    int min = head.data;
  
    // check that while head not equal to null.
    while (head != null) {
  
        // if min value is greater then head.data
        // then it breaks the while loop and
        // return the value of count.
        if (min > head.data)
            break;
  
        count++;
  
        // head assign the next value of head.
        head = head.next;
    }
    return count;
}
  
// Function to push element in linked list.
static Node push(Node head, int data)
{
    // Allocate dynamic memory for newNode.
    Node newNode = new Node();
  
    // Assign the data into newNode.
    newNode.data = data;
  
    // newNode.next assign the address of
    // head node.
    newNode.next = (head);
  
    // newNode become the headNode.
    (head) = newNode;
    return head;
}
  
// Display linked list.
static void printList(Node node)
{
    while (node != null) {
        System.out.printf("%d ", node.data);
        node = node.next;
    }
}
  
// Driver functions
public static void main(String[] args)
{
    
    // Create a node and initialize with null
    Node head = null;
  
    // push() insert node in linked list.
    // 15.18.5.8.11.12
    head = push(head, 12);
    head = push(head, 11);
    head = push(head, 8);
    head = push(head, 5);
    head = push(head, 18);
    head = push(head, 15);
  
    printList(head);
    System.out.println();
    System.out.print("Linked list rotated elements: ");
  
    // Function call countRotation()
    System.out.print(countRotation(head) +"
");
  
}
}
  
// This code contributed by gauravrajput1



输出
15 18 5 8 11 12 
Linked list rotated elements: 2

有关详细信息,请参阅有关排序和旋转链表中的计数旋转的完整文章!