思路:设置两个一个奇数链表头(其实就是head),一个偶数链表头(head.next),奇连偶,偶连奇。一直循环,最后奇偶相连
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head == null) return null;
ListNode odd = head;
ListNode even = head.next, evenHead = head.next;
while(even != null && even.next != null){
// 奇连偶
odd.next = even.next;
// 更新位置
odd = odd.next;
// 偶连奇
even.next = odd.next;
even = even.next;
}
// 奇偶相连
odd.next = evenHead;
return head;
}
}