1
我想按順時針旋轉我的鏈接列表。順時針旋轉鏈接列表
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;
}
我設法讓我的逆時針旋轉的工作,但我有點困惑如何獲得順時針旋轉,因爲我無法找到以前的節點。
我會保持Node類,我會創建一個ListNode類,實現FIFO的。然後,我將使用隊列(),dequeue()來獲取最後一個節點並將其放入隊列的起始位置。告訴我你是否想寫一個例子 –
一個例子會有幫助。 – FiftySentos