我正在尝试对链接列表类进行插入排序以按升序排序。我不确定我需要做什么。我找不到回到列表开头的方法。
public static void LLInsertionSort (LinkedList LL){
IntNode currentNode = head;
IntNode tail = null;
while(currentNode != null&& tail != head ){
while (currentNode.getData() > currentNode.getNext().getData()){
int temp = currentNode.getData();
currentNode.setData(currentNode.getNext().getData());
currentNode.getNext().setData(temp);
答案 0 :(得分:0)
您需要每次从列表中第一个节点开始。
public static IntList LLInsertionSort(Node head)
{
IntNode current = head;
IntNode tail = null;
while(current != null&& tail != head )
{
IntNode next = current;
for( ; next.next != tail; next = next.next)
{
if(next.value <= next.next.value)
{
int temp = next.value;
next.value = next.next.value;
next.next.value = temp;
}
}
tail = next;
current = head;
}
return this;
}