Absolute O(1) space JAVA


  • 0
    Z
    public class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head == null || head.next == null)
                return head;
            ListNode pre, cur,next;
            pre = head;
            cur = head.next;
            while(cur != null && cur.next != null){
                next = cur.next;
                cur.next = next.next;
                next.next = pre.next;
                pre.next = next;
                pre = pre.next;
                cur = cur.next;
            }
            return head;
        }
    }
    

Log in to reply
 

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