• 链表
  • 双指针
  • 哑节点

328. 奇偶链表

这题双指针,配合哑节点,进行移动,将一个链表重新组装成两个链表,最后偶链表接在奇链表的后面。需要注意的是指针移动的条件,不满足偶数节点为空或偶数节点下节点为空 这种情况下才进行移动。
!(pOu == null || pOu.next == null)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}

ListNode pJi = head;
ListNode pOu = head.next;

ListNode dummy1 = new ListNode();
ListNode dummy2 = new ListNode();
ListNode tail1 =dummy1;
ListNode tail2 = dummy2;

while(true){

tail1.next = pJi;
tail2.next = pOu;
tail1 = tail1.next;
tail2 = tail2.next;

if( !(pOu == null || pOu.next == null) ){
pJi = pJi.next.next;
pOu = pOu.next.next;
}else{
break;
}
tail1.next = null;
tail2.next = null;
}
tail1.next = dummy2.next;
return dummy1.next;

}
}