题目
思路
- 结点1作为奇数链的头 结点2作为偶数链的头,从第3个点开始遍历,依次轮流附在奇、偶链的后面遍历完后,奇数链的尾连向偶链的头,偶链的尾为空, 返回奇数链的头。
代码
奇偶链表public ListNode oddEvenList(ListNode head) {if (head == null || head.next == null) {return head;}ListNode p1 = head, p2 = head.next, cur = head.next;while (cur != null && cur.next != null) {p1.next = p1.next.next;cur.next = p1.next.next;p1 = p1.next;cur = cur.next;}p1.next = p2;return head;}
