Solution in Swift


  • 0
    S

    func swapPairs(_ head: ListNode?) -> ListNode? {
    if head == nil || head?.next == nil {
    return head
    }
    var cur = head
    var next = head?.next

    while next != nil {
        let temp = next?.val
        next?.val = (cur?.val)!
        cur?.val = temp!
        cur = next?.next
        next = cur?.next
    }
    return head
    }

Log in to reply
 

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