Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 顺时针旋转链表_Java_Linked List_Rotation_Singly Linked List - Fatal编程技术网

Java 顺时针旋转链表

Java 顺时针旋转链表,java,linked-list,rotation,singly-linked-list,Java,Linked List,Rotation,Singly Linked List,我想顺时针旋转我的链表一定量 private class Node { private T data; // Entry in bag private Node next; // link to next node private Node(T dataPortion) { this(dataPortion, null); } // end constructor private Node(T dataPortion, Node n

我想顺时针旋转我的链表一定量

 private class Node {

    private T data; // Entry in bag
    private Node next; // link to next node

    private Node(T dataPortion) {
        this(dataPortion, null);
    } // end constructor

    private Node(T dataPortion, Node nextNode) {
        data = dataPortion;
        next = nextNode;
    } // end constructor
} // end Node

public void leftShift(int num){

    if (num == 0) return;

    Node current  = firstNode;

    int count = 1;
    while (count < num && current !=  null)
    {
        current = current.next;
        count++;
    }

    if (current == null)
        return;


    Node kthNode = current;


    while (current.next != null)
        current = current.next;



    current.next = firstNode;


    firstNode = kthNode.next;


    kthNode.next = null;

}
私有类节点{
私有T数据;//包中的条目
私有节点下一步;//链接到下一个节点
专用节点(T数据部分){
这个(数据部分,空);
}//结束构造函数
专用节点(T数据部分,节点下一个节点){
数据=数据部分;
next=nextNode;
}//结束构造函数
}//结束节点
公共void leftShift(int num){
如果(num==0)返回;
节点当前=第一个节点;
整数计数=1;
while(计数
我设法实现了逆时针旋转,但由于找不到以前的节点,我对如何实现顺时针旋转感到困惑。

您所问的示例:

    private class Node {

    private T data; // Entry in bag
    private Node next; // link to next node

    public Node(T dataPortion) {
        this(dataPortion, null);
    } // end constructor

    public Node(T dataPortion, Node nextNode) {
        data = dataPortion;
        next = nextNode;
    } // end constructor
  T getObject() {
    return data; 
  }
  Node<T> getNext() {
    return next; 
  }
} // end Node

public class Queue<T>{
 
    private Node head;
    private Node tail;
    private String name;


    public Queue(){
        this("queue");
    }
 
    public Queue(String listName) {
        name = listName;
        head = tail = null;
    }

    public boolean isEmpty() {
        return tail == null; 
    }

    public void put(T item) {
        Node node = new Node(item);

        if (isEmpty()) // head and tail refer to same object
            head = tail = node;
        else { // head refers to new node
            Node oldtail= tail;
            tail=node;
            oldtail.nextNode=tail;

        }
    }

    public Object get() throws NoSuchElementException {
        if (isEmpty()) // throw exception if List is empty
            throw new NoSuchElementException();
 
        T removedItem = head.data; // retrieve data being removed
 
        // update references head and tail
        if (head == tail)
            head = tail = null;
        else // locate new last node
        {
            head=head.nextNode;


        } // end else
 
        return removedItem; // return removed node data
    }
    public int size() {
      int count = 0;
      if(isEmpty()) return count;
      else{
        Node<T> current = head;

        // loop while current node does not refer to tail
        while (current != null){
            count++;
            if(current.nextNode==null)break;
            current=current.nextNode;
        }

        return count;
    }
    public void shift(){
      if(size()<=1)return;

      T removed = get();
      put(removed);
    }
}
私有类节点{
私有T数据;//包中的条目
私有节点下一步;//链接到下一个节点
公共节点(T数据部分){
这个(数据部分,空);
}//结束构造函数
公共节点(T数据部分,节点下一个节点){
数据=数据部分;
next=nextNode;
}//结束构造函数
T getObject(){
返回数据;
}
节点getNext(){
下一步返回;
}
}//结束节点
公共类队列{
 
专用节点头;
私有节点尾部;
私有字符串名称;
公共队列(){
此(“队列”);
}
 
公共队列(字符串列表名){
名称=列表名称;
头=尾=空;
}
公共布尔值为空(){
返回tail==null;
}
公众认股权证(T项){
节点=新节点(项目);
if(isEmpty())//头和尾引用同一个对象
头=尾=节点;
else{//头引用新节点
节点oldtail=尾部;
尾=节点;
oldtail.nextNode=tail;
}
}
公共对象get()抛出NoTouchElementException{
if(isEmpty())//如果列表为空,则引发异常
抛出新的NoTouchElementException();
 
T removedItem=head.data;//检索正在删除的数据
 
//从头到尾更新引用
如果(头=尾)
头=尾=空;
else//找到新的最后一个节点
{
head=head.nextNode;
}//结束其他
 
return removedItem;//返回删除的节点数据
}
公共整数大小(){
整数计数=0;
if(isEmpty())返回计数;
否则{
节点电流=头;
//循环,而当前节点不引用尾部
while(当前!=null){
计数++;
if(current.nextNode==null)中断;
current=current.nextNode;
}
返回计数;
}
公共空间转移(){

如果(size(),我将保留Node类,并创建一个ListNode类,即FIFO的实现。然后,我将使用queue(),dequeue()获取最后一个节点并将其放在队列的开头。请告诉我是否要编写一个示例,一个示例将非常有用。虽然此代码可能会回答此问题,但提供有关如何以及为什么解决此问题的其他上下文将提高答案的长期价值。
ListNode* Solution::rotateRight(ListNode* A, int B) {
    if(A==NULL) return NULL;
        ListNode *cur=A;
        int len=1;
        while(cur->next!=NULL){
            cur=cur->next;
            len++;
        }
        cur->next=A;
        int preLen=len-B%len-1;
        ListNode *pre=A;
        while(preLen--)
            pre=pre->next;
        A=pre->next;
        pre->next=NULL;
        return A;
}