维护两个指针,一个指向奇数链表的头,一个指向偶数链表的头
先将链表分为奇数链表和偶数链表,然后再进行合并。
奇数位置的后面必定是偶数,偶数位置后面必定是奇数,利用这个特点即可解决这道题目~
public ListNode oddEvenList (ListNode head) { if (head == null || head.next == null) return head; ListNode odd = head; ListNode oddHead = head; ListNode even = head.next; ListNode 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 oddHead; }