Intuitive Ruby Solution


  • 0
    R
    def odd_even_list(head)
        return head if head.nil? || head.next.nil?
        
        even_head, even_tail, odd_head, odd_tail, runner = head.next, head.next, head, head, head.next.next
        count = 1
        while(runner) do
           if count % 2 == 0
               even_tail.next, even_tail, runner = runner, runner, runner.next
           else
               odd_tail.next, odd_tail, runner = runner, runner, runner.next 
           end
            count += 1
        end
        odd_tail.next = even_head
        even_tail.next = nil
        head
    end
    

Log in to reply
 

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