[JavaScript] leetCode : Linked List Cycle II
728x90

๋ฌธ์ œ

Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.

Do not modify the linked list.

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์‚ฌ์ดํด์„ ๊ฐ–๊ณ  ์žˆ์„ ๋•Œ ์‚ฌ์ดํด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 

ํ’€์ด

const detectCycle = function(head) {

    if(head === null) return null;

    // ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ๋‘”๋‹ค
    let fast = head; // ๋‘ ์นธ์”ฉ 
    let slow = head; // ํ•œ ์นธ์”ฉ

    while(fast && fast.next){ 
        fast = fast.next.next;
        slow = slow.next;

        // ๋‘˜์ด ๊ฐ™์•„์ง€์ง„๋‹ค === ์‚ฌ์ดํด์ด ์กด์žฌํ•œ๋‹ค
        // ๋งŒ๋‚œ ์ง€์  ==> ์‚ฌ์ดํด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„
        if(fast === slow){
            fast = head;
            while(fast !== slow){
                slow = slow.next;
                fast = fast.next;
            }
            return fast;
        }
    }

    return null;    
};

 

 

์ด ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ์ฑ•ํ„ฐ์—์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ํˆฌ ํฌ์ธํ„ฐ ๊ธฐ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜๊ณ  ์žˆ์–ด์„œ, ์ฝ์–ด ๋ณธ ํ›„ ์ ์šฉ์„ ์‹œ์ผœ๋ณด๋ ค ํ–ˆ๋‹ค.

fast์— ํ•ด๋‹นํ•˜๋Š” node๊ฐ€ null์ผ ๊ฒฝ์šฐ๊ฐ€ ๋จผ์ € ๋– ์˜ฌ๋ผ์„œ ์กฐ๊ฑด๋ฌธ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘์—ˆ์„ ๋•Œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๊ณ„์† ๋– ์„œ ๊ฒฐ๊ตญ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด์˜ ๋„์›€์„ ์–ป๊ฒŒ ๋˜์—ˆ๋‹ค. ๐Ÿ˜‡.. 

์ด ์—๋Ÿฌ์˜ ์›์ธ์€ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์— ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ ๊ฒƒ์œผ๋กœ ์ถ”์ธก.. 

while(fast !== null && fast.next !== null) 

 

 

 

ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ 

์šฐ์„  ์œ„์™€ ๊ฐ™์ด ๋‘ ๊ฐœ์˜ ์†๋„๊ฐ€ ๋‹ค๋ฅธ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์ดํด์˜ ์กด์žฌ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์„ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜(ํ”Œ๋กœ์ด๋“œ์˜ ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ Floyd's tortoise and hare)์ด๋ผ๊ณ  ํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

 

๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๊ฐ€ ์กด์žฌํ•˜๊ณ  head๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ์„œ๋กœ ๋‹ค๋ฅธ ์†๋„๋กœ ๊ฐ„๋‹ค๊ณ  ํ•˜์ž.


1๋ฒˆ ํฌ์ธํ„ฐ(ํ† ๋ผ)์™€ 2๋ฒˆ ํฌ์ธํ„ฐ(๊ฑฐ๋ถ์ด). 1๋ฒˆ ํฌ์ธํ„ฐ๋Š” ํ•œ ๊ฐœ์˜ ๋…ธ๋“œ์”ฉ, 2๋ฒˆ ํฌ์ธํ„ฐ๋Š” ๋‘ ๊ฐœ์˜ ๋…ธ๋“œ์”ฉ ๊ฐ„๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ,

  • ์‚ฌ์ดํด์ด ์—†๋‹ค : ๋น ๋ฅธ ํฌ์ธํ„ฐ๊ฐ€ ๋จผ์ € ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋์— ๋„์ฐฉํ–ˆ์„ ๊ฒฝ์šฐ
  • ์‚ฌ์ดํด์ด ์žˆ๋‹ค : ๋น ๋ฅธ ํฌ์ธํ„ฐ๊ฐ€ ํ•œ ๋ฐ”ํ€ด ๋Œ๊ณ ๋‚˜์„œ ๋Š๋ฆฐ ํฌ์ธํ„ฐ์™€ ๋งŒ๋‚ฌ์„ ๋•Œ

์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋ฆผ์œผ๋กœ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์œ„์™€ ๊ฐ™์ด ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๋งŒ๋‚˜๋Š” ์ง€์ ์ด ์žˆ์„ ๋•Œ ์‚ฌ์ดํด์ด ์กด์žฌํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋Ÿฐ๋„ˆ ๊ธฐ๋ฒ•

while fast && fast.next

๊ทธ๋ ‡๋‹ค๋ฉด ์œ„ ์กฐ๊ฑด์€ ๋ฌด์—‡์ธ๊ฐ€?
๊ฒ€์ƒ‰์„ ํ•˜๋‹ค๊ฐ€ ํŒŒ์ด์ฌ์—์„œ ์ด์™€ ๋น„์Šทํ•˜๊ฒŒ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ์†๋„๊ฐ€ ๋‹ค๋ฅธ ๋‘ ๊ฐœ์˜ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋Ÿฐ๋„ˆ Runner ๊ธฐ๋ฒ•์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์•„๋งˆ ํฌ์ธํ„ฐ์˜ ์‚ฌ์šฉ ๋ฐฉ์‹์ด ๊ฑฐ์˜ ๋น„์Šทํ•˜์—ฌ ์กฐ๊ฑด์ด ๊ฐ™์€ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ถ”์ธก์„ ํ•ด๋ณธ๋‹ค.


๋น ๋ฅธ ๋Ÿฐ๋„ˆ(fast)๊ฐ€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋์— ๋„๋‹ฌํ•˜๋ฉด ๋Š๋ฆฐ ๋Ÿฐ๋„ˆ(slow)๋Š” ์ •ํ™•ํžˆ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„ ์ง€์ ์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.

์ด ๊ธฐ๋ฒ•์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ณ  ์ค‘๊ฐ„์ง€์ ๊นŒ์ง€๋งŒ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์†Œ์š” ์‹œ๊ฐ„์„ ๋งŽ์ด ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

fast ๋ณ€์ˆ˜๊ฐ€ ๋จผ์ € ๋ฆฌ์ŠคํŠธ์˜ ๋์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€๋งŒ ์ฐธ์ผ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์œ„ ์กฐ๊ฑด์€ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜๋ƒ, ํ™€์ˆ˜๋ƒ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค.

 

๋…ธ๋“œ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜์ธ ๊ฒฝ์šฐ ๋ ๋ถ€๋ถ„์—์„œ fast๋Š” null ์ƒํƒœ์ด๊ณ , slow๋Š” ๋”ฑ ์ค‘๊ฐ„ ์ง€์ ๊นŒ์ง€ ์™”๋‹ค.

  1 2 3 4 null
fast         fast
slow     slow    

 

๋…ธ๋“œ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ fast๋Š” 4์— ์žˆ์ง€๋งŒ fast.next๋Š” null ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๋”ฐ๋ผ์„œ while fast && fast.next ์˜ ์กฐ๊ฑด ์ˆœ์„œ๋„ ์ค‘์š”ํ•˜๋‹ค.

  1 2 3 2 4 null
fast         fast  
slow     slow      

 

 

์ด๋Ÿฐ ๊ณผ์ •์„ ์‚ฌ์ดํด์ด ์กด์žฌ ํ•  ๋•Œ, ๋‘ ํฌ์ธํ„ฐ๋กœ ์‚ฌ์ดํด์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
์ด ์œ„์น˜๊ฐ€ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ธ๋ฑ์Šค์ด๋‹ค.

๋‘˜ ์ค‘ ํ•œ ํฌ์ธํ„ฐ๋ฅผ head์— ๋‘๊ณ  ์ด๋ฒˆ์—๋Š” ๋‘ ํฌ์ธํ„ฐ ๋ชจ๋‘ ํ•œ ์นธ์”ฉ๋งŒ ์›€์ง์ด๋„๋ก ํ•œ๋‹ค.

 

ํ•œ ์นธ์”ฉ ์›€์ง์ด๋‹ค๋ณด๋ฉด ๋‘ ํฌ์ธํ„ฐ๊ฐ€ ๋งŒ๋‚˜๋Š” ์ง€์ ์ด ์žˆ๋‹ค.

์ด ๋ถ€๋ถ„์ด ๋ฐ”๋กœ ์‚ฌ์ดํด ์‹œ์ž‘์ง€์ ์ด๋‹ค. 

 

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ด€๋ จํ•ด์„œ ์ˆ˜ํ•™ ๊ณต์‹์ด ์žˆ๋Š”๋ฐ, ๊ณต์‹์„ ์ข€ ๋” ๋“ค์—ฌ๋‹ค๋ณธ ํ›„ ์ดํ•ดํ•œ ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•˜๊ณ .. ๋‹ค๋ฅธ ๋ฐฉ์‹์˜ ํ’€์ด๋„ ์กด์žฌํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„ ์ถ”๊ฐ€ํ•ด์•ผ๊ฒ ๋‹ค ๐Ÿ˜‡

 

 

์ฐธ์กฐํ•œ ๋ธ”๋กœ๊ทธ๋“ค.

  • ํ† ๋ผ์™€ ๊ฑฐ๋ถ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ด€ํ•˜์—ฌ

https://dev-note-97.tistory.com/277?category=948582 

 

[๋ฆฌํŠธ์ฝ”๋“œ] 142. Linked List Cycle II / Javascript

๋ฌธ์ œ์ฃผ์†Œ :https://leetcode.com/problems/linked-list-cycle-ii/ Linked List Cycle II - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and..

dev-note-97.tistory.com

 

 

  • while๋ฌธ์˜ ์กฐ๊ฑด์— ๊ด€ํ•˜์—ฌ

https://blog.naver.com/fblod/222634013592

 

LeetCode 234: Palindrome Linked List

Palindrome Linked List - LeetCode Given the head of a singly linked list, return true if it is...

blog.naver.com

 

728x90

'๐Ÿ““ Algorithm > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JavaScript] leetCode : Implement strStr()  (0) 2022.05.12
[JavaScript] leetCode : Longest Common Prefix  (0) 2022.05.09
[JavaScript] leetCode : Valid Mountain Array  (3) 2022.05.02
[JavaScript] LeetCode : Add Binary  (0) 2022.04.22
[JavaScript] LeetCode : Plus One  (0) 2022.04.21