-1
public void reverse(int index) {
if (first == null) {
return;
}
int count = 0;
Node current = first;
Node previous = null;
Node next;
while (current != null && count < index && index> 0) {
next = current.next;
current.next = previous;
previous = current;
current = next;
count++;
}
first = previous;
}
这是我的代码。所以我想颠倒一个链表并停止索引处的逆向过程。例如,假设我有{One Two Three Four Five},索引是3.所以输出结果是{Three Two One Four Five}。但对于我的代码,我能够在给定索引之前将数据反转,但由于某种原因,输出变为{Three Two One}。如何在给定索引处反转时保留其余数据?如何在指定索引处反转链接列表?