Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

30-InSange #100

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

30-InSange #100

wants to merge 3 commits into from

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Sep 6, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Delete Nodes From Linked List Present in Array

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ฌธ์ œ ๊ฐœ์š”

์ •์ˆ˜ํ˜• ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ head์™€ ์ •์ˆ˜ ๋ฐฐ์—ด nums๊ฐ€ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ nums์•ˆ์— ์†ํ•œ ๋…ธ๋“œ๋“ค์„ ๋บ€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ํ’€์ด ๋ฐฉ์‹

๋ฌธ์ œ๋ฅผ ์ž˜ ๋ชป ์ดํ•ดํ•˜์˜€๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํฐ ํ‹€์€ ๋‹ฌ๋ผ์ง„๊ฒŒ ์—†์–ด์„œ ๊ธˆ๋ฐฉ ๊ณ ์น  ์ˆ˜ ์žˆ์—ˆ์Œ.
์ฒ˜์Œ์— nums์— ์†ํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ์ง€๋‹Œ ๋…ธ๋“œ๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ์ค„ ์•Œ์•˜๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์—ˆ์„ ๋•Œ ๋ฐ˜๋Œ€์˜€๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์Œ.

  1. nums์— ์†ํ•˜๋Š” ๊ฐ’๋“ค์„ ๋งต์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋ถ„๋ฅ˜ํ•ด์ค€๋‹ค. (um์— ์กด์žฌํ•˜๋Š” ๊ฐ’ = ๋นผ์•ผํ•˜๋Š” ๊ฐ’)
  2. ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ um์— ์†ํ•œ ๊ฐ’์ผ ๊ฒฝ์šฐ removeList์˜ next ๋…ธ๋“œ ๊ฐ’์„ ํ˜„์žฌ head๊ฐ’์œผ๋กœ ์„ธํŒ…ํ•ด์ฃผ๊ณ  ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.
  3. ๋งŒ์•ฝ removeList๊ฐ€ nullptr์ผ ๊ฒฝ์šฐ ํ˜„์žฌ head๋ฅผ ์„ธํŒ….
  4. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ. ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰.
class Solution {
public:
    ListNode* modifiedList(vector<int>& nums, ListNode* head) {
        ListNode* removeList = nullptr;
        ListNode* removeHead = nullptr;
        unordered_map<int, bool> um;
        
        for(int& val : nums) um[val] = true;
        
        while(head != nullptr)
        {
            cout << head->val << " ";
            if(um[head->val] == false)
            {
                if(removeList) 
                {
                    cout << "removeHead = " << removeList->val << " ";
                    removeList->next = head;
                    removeList = removeList->next;
                }
                else 
                {
                    removeList = head;
                    removeHead = removeList;
                }
            }
            head = head->next;
            if(removeList) removeList->next = nullptr;
        }
        
        return removeHead;
    }
};

์ฒ˜์Œ์— ์• ๋ฅผ ๋จน์—ˆ๋˜๊ฒŒ

 removeList = removeList->next;

removeList์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ์‹œ์ผœ์ค˜์•ผ ๋œ๋‹ค.
๊ฐฑ์‹  ์‹œ์ผœ์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋งจ ์ฒ˜์Œ ๋“ค์–ด์˜จ head๊ฐ’๊ณผ head->next ๊ฐ’ ๋‘ ๊ฐœ๋งŒ ํƒ์ƒ‰์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
๊ทธ๋ ‡๋‹ค๊ณ  ๋งˆ์ง€๋ง‰์— head๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค„ ๋•Œ ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ๋ฌดํ•œ ๋ฃจํ”„๊ฐ€ ๋œ๋‹ค.

๊ทธ๋Ÿผ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•˜๋ฉด ๋˜์ง€ ์•Š๋Š๋ƒ. ๊ทธ๋Ÿผ ๊ทธ๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ๋น„์ค‘์ด ๋” ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ ์ฃผ์–ด์ง„ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋…ธ๋“œ๋งŒ ์จ์„œ ํ™œ์šฉํ•ด๋ณด๊ณ  ์‹ถ์—ˆ๋‹ค๊ฐ€ ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ๋‹ค.

image

๋‘๋ฒˆ์งธ ํ’€์ด

๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ์ƒ๊ฐํ•ด๋ณด๋‹ค๊ฐ€ ์–ด์ฐจํ”ผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋…ธ๋“œ๋งŒ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋Š” ๊ฒƒ์ด๊ณ  um์— ํ•ด๋‹นํ•˜๋Š”, nums ๋ฒˆํ˜ธ์— ์†ํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ทธ๋ƒฅ ๋„˜๊ฒจ๋ฒ„๋ฆฌ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ? ์ƒ๊ฐ์ด ๋– ์˜ฌ๋ž๋‹ค.

๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ์žฌํ™œ์šฉํ•˜์—ฌ

removeList์˜ head๋ฅผ ๋นˆ ๋…ธ๋“œ ๊ฐ’์œผ๋กœ ์ฑ„์›Œ์ฃผ์—ˆ๋‹ค.
์ด ๋ถ€๋ถ„์€ ์ถ”ํ›„ ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋น„๊ตํ•  ๋•Œ ์กฐ๊ฑด๋ฌธ์„ ํ˜„์žฌ๋…ธ๋“œ -> next๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๋นˆ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ ํ•ด์ฃผ์—ˆ๋‹ค.

removeList์˜ next๋ฅผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ head๋กœ ์„ค์ •ํ•œ ํ›„ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค.

  1. ํ˜„์žฌ๋…ธ๋“œ -> next์˜ ๊ฐ’์ด ์กด์žฌํ•  ๊ฒฝ์šฐ. ํ•ด๋‹น next์˜ ๊ฐ’์ด nums์— ์†ํ•  ๊ฒฝ์šฐ. ์•„์˜ˆ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ์‹œ์ผœ์ค€๋‹ค.
  2. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ทธ๋ƒฅ ํƒ์ƒ‰
class Solution {
public:
    ListNode* modifiedList(vector<int>& nums, ListNode* head) {
        ListNode* removeList = new ListNode();
        ListNode* removeHead;
        unordered_map<int, bool> um;
        
        for(int& val : nums) um[val] = true;
        
        removeList->next = head;
        removeHead = removeList;

        while(removeHead->next)
        {
            if(um[removeHead->next->val] == true) removeHead->next = removeHead->next->next;
            else removeHead = removeHead->next;
        }
        
        return removeList->next;
    }
};

๋” ๊ฐ„๊ฒฐํ•ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ

image

ํšจ์œจ๋„ ๋” ์˜ฌ๋ผ๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹จ์ˆœํ•˜๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

9์›” 11์ผ ๊นŒ์ง€๋Š” ๋‹น๋ถ„๊ฐ„ ์บก๋”” ๊ฐœ๋ฐœ๋กœ ๋ฐ”๋น ์„œ ํž˜๋“ค ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..! ๋ฆฌ๋ทฐ๋Š” ์ตœ๋Œ€ํ•œ ์‹œ๊ฐ„ ์งฌ๋‚ผ๋•Œ๋งˆ๋‹ค ํ•˜๊ฒ ์œผ๋‚˜ ์ž ์‹œ PR์€ ๋ฏธ๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant