leetcode Odd Even Linked List

题目链接

这个题没啥难的注意边界条件就好

class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }

public class Solution {
    public ListNode oddEvenList(ListNode head) {
        if(head==null)
        {
            return null;
        }
        if(head.next==null)
        {
            return head;
        }


        boolean isodd=true;
        ListNode odd=head;
        ListNode even=head.next;
        ListNode oddhead=head;
        ListNode evenhead=head.next;
        head=head.next.next;
        while(head!=null)
        {
            if(isodd)
            {

                odd.next=head;
                odd=odd.next;
            }
            else
            {
                even.next=head;
                even=even.next;
            }
            isodd=!isodd;
        }
        odd.next=evenhead;
        even.next=null;
        return oddhead;
    }
}

你可能感兴趣的:(leetcode Odd Even Linked List)