【leetcode】143.(Medium)Reorder List
解题思路:
蛮力循环提交代码:
class Solution {
public void reorderList(ListNode head) {
if(head==null||head.next==null||head.next.next==null) return;
int len=1;
ListNode p1=head,p2=head;
while(p2.next!=null) {
p2=p2.next;
len++;
}
ListNode tmp=head.next;
p1.next=p2;
p2.next=tmp;
len-=2;
while(len>0) {
p1=p2.next;
p2=p1;
for(int i=1;i<len;i++)
p2=p2.next;
tmp=p1.next;
p1.next=p2;
p2.next=tmp;
len-=2;
}
p2.next=null;
}
}
运行结果: