标签:
/* * 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; }
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5656951.html