본문 바로가기
Leetcode

Leetcode 328. Odd Even Linked List 52.5% Medium

by tiit 2020. 4. 3.
반응형

스터디 문제4번 LinkedList 정답률 52.5%

 

https://blog.naver.com/1ilsang/221597552829

 

LeetCode 328. Odd Even Linked List - LinkedList(in Place)

https://leetcode.com/problems/odd-even-linked-list/submissions/링크드 리스트 문제인데, O(1) 의 공간...

blog.naver.com

 

class Solution {
     public ListNode oddEvenList(ListNode head) {
        ListNode odd = head;
        if(odd == null) return head;
        ListNode even = head.next;
        if(even == null || even.next == null) return head;
        go(head.next.next, odd, even, even, 3);
        return odd;
    }
    
    private void go(ListNode head, ListNode odd, ListNode even, ListNode firstEven, int depth) {
        if(head == null) {
            even.next = null;
            odd.next = firstEven;
            return;
        }
        if(depth % 2 == 0) {
            even.next = head;
            even = even.next;
        } else {
            odd.next = head;
            odd = odd.next;
        }
        go(head.next, odd, even, firstEven, depth + 1);

    }
}

반응형

댓글