1 import java.util.*; 2 3 /* 4 * public class ListNode { 5 * int val; 6 * ListNode next = null; 7 * public ListNode(int val) { 8 * this.val = val; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 15 * @param head ListNode类 16 * @return ListNode类 17 */ 18 public ListNode oddEvenList (ListNode head) { 19 // 判空链表 20 if (head == null) { 21 return head; 22 } 23 // 奇数链表的指针 24 ListNode one = head; 25 // 偶数链表的指针 26 ListNode two = head.next; 27 // 临时指针记录偶数链表的头指针-用于最后奇偶相连 28 ListNode temp = two; 29 while (two != null && two.next != null) { 30 one.next = two.next; 31 one = two.next; 32 two.next = one.next; 33 two = one.next; 34 } 35 // 奇偶链表相连 36 one.next = temp; 37 // 返回结果 38 return head; 39 } 40 }
标签:奇偶,head,ListNode,14,two,next,链表 From: https://www.cnblogs.com/StringBuilder/p/17837471.html