1ms Java Solution


  • 30
    P
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head == null || head.next == null)
                return head;
            ListNode odd = head;
            ListNode even = head.next;
            ListNode evenHead = even;
            while(odd.next != null && even.next != null){
                odd.next = even.next;
                odd = odd.next;
                even.next = odd.next;
                even = even.next;
            }
            odd.next = evenHead;
            return head;
        }
    }

  • 0
    E
    This post is deleted!

  • 0
    J

    smart solution!!!Thank you!


  • -10
    J

    This is not O(1) space complexity.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.