题目传送: https://leetcode.cn/problems/reorder-list/
运行效率
代码如下:
class Solution {
public void reorderList(ListNode head) {
//双端队列
ArrayDeque<ListNode> deque = new ArrayDeque<>();
ListNode cur=head;
while(cur != null){
deque.add(cur);
cur = cur.next;
}
int index=1;
head=deque.poll();
cur=head;
while(!deque.isEmpty()){
if(index%2==0){
//取出队列的头部元素
ListNode poll = deque.poll();
cur.next=poll;
}else{
//取出队列的尾部元素
ListNode listNode = deque.pollLast();
cur.next=listNode;
}
index++;
cur=cur.next;
}
cur.next=null;
}
}