数据结构:数组
算法:使用数组保存链表的每个节点,按规律取出并重连链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
ListNode phead = new ListNode(-1,head),p = phead.next;
ArrayList<ListNode> plist = new ArrayList<>();
while(p != null){
plist.add(p);
p = p.next;
}
int flag = 0;
p = phead.next;
for(int i = 1, j = plist.size()-1; i <= j; ){
if(flag == 0){
p.next = plist.get(j);
j--;
flag = 1;
}
else{
p.next = plist.get(i);
i++;
flag = 0;
}
p = p.next;
}
p.next = null;
}
}