【发布时间】:2014-02-23 06:31:31
【问题描述】:
问题如下:
给定linked list,将备用indices 移到list 的后面
例如:
input: : [0] -> [1] -> [2] -> [3] -> [4] -> [5] -> [6] -> [7]
expected output: [0] -> [2] -> [4] -> [6] -> [1] -> [3] -> [5] -> [7] /
从预期的输出中可以看出,奇数位置(索引)的元素被移动到linkedlist 的后面。我试图实现这一点;我可以删除奇数索引,但它们没有链接到列表的末尾。
我的代码在这里:
public void shift(){
if (front==null) return;
ListNode curr=front;
ListNode temp=curr.next;
while (curr.next!=null && curr.next.next!=null){
curr.next=curr.next.next;
curr=curr.next;
temp.next=curr.next;
}
curr.next=temp;
temp.next=null;
}
expected output: front -> [0] -> [2] -> [4] -> [6] -> [1] -> [3] -> [5] -> [7] /
my output: front -> [0] -> [2] -> [4] -> [6] -> [1] /
我需要一些帮助
P.S:不得使用辅助存储。没有其他容器!!!所以这是一个就地重新安排
【问题讨论】:
标签: java algorithm linked-list singly-linked-list