Kotlin


  • 0
    J
    class Solution {
        fun swapPairs(head: ListNode?): ListNode? {
            val seq = generateSequence(head) { it.next }
            fun swapNode(node1: ListNode, node2: ListNode) {
                val temp = node1.`val`
                node1.`val` = node2.`val`
                node2.`val` = temp
            }
    
            seq.fold<ListNode, ListNode?>(null) { acc, listNode -> acc?.also { swapNode(acc, listNode) }; if (acc == null) listNode else null}
            return head
        }
    }
    

Log in to reply
 

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