<commit_before>package katas.kotlin.leetcode.swap_nodes_in_pairs

import katas.kotlin.leetcode.ListNode
import katas.kotlin.leetcode.listNodes
import kotlincommon.test.shouldEqual
import org.junit.Test

/**
 * https://leetcode.com/problems/swap-nodes-in-pairs
 */
class SwapNodesInPairsTests {
    @Test fun `swap every two adjacent nodes`() {
        listNodes(1, 2).swapPairs() shouldEqual listNodes(2, 1)
        listNodes(1, 2, 3, 4).swapPairs() shouldEqual listNodes(2, 1, 4, 3)
    }
}

private fun ListNode.swapPairs(): ListNode {
    var first = this
    var second = first.next!!
    var third = first.next!!.next
    val head = second

    second.next = first
    first.next = third?.next

    while (third != null) {
        first = third
        second = first.next!!
        third  = second.next

        second.next = first
        first.next = third
    }

    return head
}<commit_msg>swap nodes<commit_after>package katas.kotlin.leetcode.swap_nodes_in_pairs

import katas.kotlin.leetcode.ListNode
import katas.kotlin.leetcode.listNodes
import kotlincommon.test.shouldEqual
import org.junit.Test

/**
 * https://leetcode.com/problems/swap-nodes-in-pairs
 */
class SwapNodesInPairsTests {
    @Test fun `swap every two adjacent nodes`() {
        listNodes(1, 2).swapPairs() shouldEqual listNodes(2, 1)
        listNodes(1, 2, 3, 4).swapPairs() shouldEqual listNodes(2, 1, 4, 3)
    }
}

private fun ListNode.swapPairs(): ListNode {
    var first = this
    var second = first.next!!
    var third = first.next!!.next
    val head = second

    second.next = first
    first.next = third?.next

    while (third != null) {
        first = third
        second = first.next!!
        third  = second.next

        second.next = first
        first.next = third?.next
    }

    return head
}
{'fraction_non_alphanumeric': 0.11412742382271468, 'fraction_numerical': 0.013296398891966758, 'mean_word_length': 2.7861635220125787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377205', 'n_tokens_mistral': 628, 'n_tokens_neox': 594, 'n_words': 139}