[LeetCode] 328. Odd Even Linked List(Kotlin)

0

LeetCode

목록 보기
20/58
post-thumbnail

[LeetCode] 328. Odd Even Linked List(Kotlin)

풀이

class Solution {
    fun oddEvenList(head: ListNode?): ListNode? {
        var oddHead = ListNode(-1)
        var evenHead = ListNode(-1)

        var i = head
        var odd = oddHead
        var even = evenHead
        var flag = true //true: odd, false: even

        while(i != null){
            if(flag) {
                odd.next = i
                odd = odd.next
            }
            else{
                even.next = i
                even = even.next
            } 
            i = i.next
            flag = !flag
        }
        odd.next = evenHead.next
        even.next = null //linked list cycle 삭제
        return oddHead.next
    }
}
profile
Be able to be vulnerable, in search of truth

0개의 댓글