[LeetCode] 24. Swap Nodes in Pairs(Kotlin)

0

LeetCode

목록 보기
8/58
post-thumbnail
post-custom-banner

[LeetCode] 24. Swap Nodes in Pairs(Kotlin)

풀이

class Solution {
    fun swapPairs(head: ListNode?): ListNode? {
        var swapHead = ListNode(-1)
        swapHead.next = head

        var i = swapHead
        while(i.next != null && i.next.next != null){
            var a = i
            var b = i.next
            var c = i.next.next

            //swap
            a.next = c
            b.next = c.next
            c.next = b

            i = i.next.next
        }
        return swapHead.next
    }
}
profile
Be able to be vulnerable, in search of truth
post-custom-banner

0개의 댓글