My Swift Solution


  • 0
    J
    func oddEvenList(head: ListNode?) -> ListNode? {
            if head?.next?.next == nil {
                return head
            }
    
            var lastOddNode = head
            let firstEvenNode = head?.next
            var lastEvenNode = head?.next
    
            while lastEvenNode?.next != nil {
                lastOddNode?.next = lastEvenNode?.next
                lastEvenNode?.next = lastOddNode?.next?.next
                lastOddNode?.next?.next = firstEvenNode
    
                lastOddNode = lastOddNode?.next
                lastEvenNode = lastEvenNode?.next
            }
    
            return head
        }

Log in to reply
 

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