题解 | #【冲刺双百】链表的奇偶重排#
链表的奇偶重排
http://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
思路
- 每次看两个,隔一个指向一个
- 注意分类讨论以及链表指向的关系
public class Solution {
public ListNode oddEvenList (ListNode head) {
if(head==null||head.next==null) return head;
ListNode now=head;
ListNode now2=head.next;
ListNode head2=now2;
while(now2.next!=null&&now2.next.next!=null){
now.next=now2.next;
now=now.next;
now2.next=now.next;
now2=now2.next;
}
if(now2.next==null){
now.next=head2;
}
else{
now.next=now2.next;
now=now.next;
now2.next=null;
now.next=head2;
}
return head;
}
}