simple ruby solution


  • 0
    T
    def odd_even_list(head)
        return head if head.nil? || head.next.nil?
        oe = head
        es = head.next
        ee = es
        start = oe
        head = head.next.next if head.next
        odd = true
        while head != nil
            if odd
                oe.next = head
                oe = oe.next
            else
                ee.next = head
                ee = ee.next
            end
            head = head.next
            odd = !odd
        end
        oe.next = es
        ee.next = nil
        return start
    end
    

Log in to reply
 

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