首页 > 其他 > 详细

328. Odd Even Linked List

时间:2016-07-10 06:12:25      阅读:280      评论:0      收藏:0      [点我收藏+]
    /*
     * 328. Odd Even Linked List
     * 2016-7-9 by Mingyang
     * 我自己的代码是做的in place并且没有用假头
     */
     public static ListNode oddEvenList1(ListNode head) {
            ListNode res=head;
            if(head==null||head.next==null)
              return res;
           ListNode next=head.next;
           ListNode pre=next;
           ListNode odd=next.next;
           while(head.next!=null&&pre!=null&&pre.next!=null){
               if(pre.next!=odd)
                  odd=pre.next;
               head.next=odd;
               pre.next=odd.next;
               odd.next=next;
               head=odd;
               pre=pre.next;
           }
           return res;
        }
     //网上的代码,虽然更简洁,但是没有我的代码清晰
     public ListNode oddEvenList(ListNode head) {
            if (head != null) {
                ListNode odd = head, even = head.next, evenHead = even; 
                while (even != null && even.next != null) {
                    odd.next = odd.next.next; 
                    even.next = even.next.next; 
                    odd = odd.next;
                    even = even.next;
                }
                odd.next = evenHead; 
            }
            return head;
        }

 

328. Odd Even Linked List

原文:http://www.cnblogs.com/zmyvszk/p/5656951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!