您的位置:首页 > 其它

Reorder List LeetCode

2014-08-10 12:38 387 查看
LeetCode :Reorder List

public void reorderList(ListNode head) {
if(head==null||head.next==null){
return ;
}
int len=0;
ListNode p=head;
ListNode end=head;
ListNode q;
ListNode next;
while(p!=null){
len++;
p=p.next;
}
for(int i=0;i<len>>1;i++){
end=end.next;
}
q=end.next;
end.next=null;
Stack<ListNode> stack = new Stack<ListNode>();
while(q!=null){
stack.push(q);
q=q.next;
}
p=head;
while(p!=null&&!stack.empty()){
q=stack.pop();
next=p.next;
p.next=q;
q.next=next;
p=next;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: