Skip to content

leet code 문제풀이

linked list 완료

1225 - Flatten a Multilevel Doubly Linked List

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* prev;
    Node* next;
    Node* child;
};
*/

class Solution {
public:
    Node* flatten(Node* head) {
        if(!head) return head;
        Node* last = head;
        Node* cur = head;
        Node* branchPtrArr[1010];
        int top = 0;

        branchPtrArr[0] = head;
        top++;

        // 그리고 child에서 탐색
        // child에서 nullptr까지 도달하면, 종료
        while (top)
        {
            while (cur)
            {
                // 만약 child가 있는 노드를 만나면
                // branchPtrArr에 cur->next 노드 추가
                if (cur->child)
                {
                    if (cur->next)
                    {
                        branchPtrArr[top] = cur->next;
                        top++;
                    }
                    cur->next = cur->child;
                    cur->next->prev = cur;
                    cur->child = nullptr;
                }

                if (!(cur->next))
                {
                    if (top == 1) top--;
                    if (top>1)
                    {
                        last = branchPtrArr[top - 1];
                        cur->next = last;
                        last->prev = cur;
                        top--;
                    }
                }
                // 오른쪽으로 가기
                cur = cur->next;
            }
        }

        return head;
    }
};

나는 코드를 좀 더 길게 썼는데, DFS에 의한 inorder 탐색으로 짧게 풀이가 가능하다고 합니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Node tail = null;
public Node flatten(Node head) {
    if(head == null) return null;

    head.prev = tail;
    tail = head;

    Node nextNode = head.next;

    head.next = flatten(head.child);
    head.child = null;

    tail.next = flatten(nextNode);

    return head;
}

1229 - Copy List with Random Pointer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;

    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        Node* orgLst[10001] = { nullptr, };
        Node* dstLst[10001] = { nullptr, };
        int orgTop = 0;
        int dstTop = 0;

        Node* ptr = head;
        while (ptr)
        {
            orgLst[orgTop] = ptr;
            orgTop++;
            ptr = ptr->next;
        }

        Node* rsltHead = new Node(0);
        ptr = head;
        Node* dstPtr = rsltHead;
        for (int i = 0; i < orgTop; ++i)
        {
            Node* newNode = new Node(ptr->val);
            dstPtr->next = newNode;

            ptr = ptr->next;
            dstPtr = dstPtr->next;
            dstLst[dstTop] = newNode;
            dstTop++;
        }

        ptr = head;
        dstPtr = rsltHead->next;
        int idx = 0;
        for (int i = 0; i < orgTop; ++i)
        {
            if (ptr->random)
            {
                idx = getIndex(orgLst, ptr->random);
                dstPtr->random = dstLst[idx];
            }

            ptr = ptr->next;
            dstPtr = dstPtr->next;
        }
        return rsltHead->next;
    }

private:
    int getIndex(Node* lst[], Node* address)
    {
        int i = 0;
        for (; i < 10001; ++i)
        {
            if (address == lst[i]) break;
        }
        return i;
    }
};