簡體   English   中英

順時針旋轉鏈接列表

[英]Rotating a Linked List Clockwise

我想將鏈表順時針旋轉一定數量。

 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;

}

我設法使逆時針旋轉可以工作,但是由於找不到以前的節點,我對如何獲得順時針旋轉感到困惑。

您詢問的示例:

    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);
    }
}
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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM