题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { public ListNode oddEvenList (ListNode head) { if(head==null||head.next==null){ return head; } ListNode n1=head; ListNode pre1=n1; ListNode n2=head.next; ListNode pre2=n2; head=head.next.next; while(head!=null&&head.next!=null){ n1.next=head; n1=n1.next; n2.next=head.next; n2=n2.next; head=head.next.next; } if(head!=null){ n1.next=head; n1=n1.next; } n1.next=pre2; n2.next=null; return pre1; } }