Swift solution


  • 0
    class Solution {
        func oddEvenList(_ head: ListNode?) -> ListNode? {
            if head == nil {
                return nil
            }
            
            var odd: ListNode? = head
            var even: ListNode? = head?.next
            let evenHead: ListNode? = even
            
            while even != nil && even?.next != nil {
                odd?.next = odd?.next?.next
                even?.next = even?.next?.next
                odd = odd?.next
                even = even?.next
            }
            odd?.next = evenHead
            
            return head
        }
    }
    

Log in to reply
 

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