본문 바로가기

알고리즘/해커랭크

[해커랭크] Find Merge Point of Two Lists

문제링크: www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/problem

 

Find Merge Point of Two Lists | HackerRank

Given two linked lists, find the node where they merge into one.

www.hackerrank.com

N*M 시간복잡도의 해결법밖에 생각나지 않았다. 구현 했더니 통과는 됐는데 찝찝한 채로 다른 사람의 풀이를 

봤다. 댓글 반응은 아주 좋았는데 코드를 봐도 어떤 풀인지 이해하는데 한참 걸렸다. 

그 풀이를 설명한 다른 블로그가 있는데 그것을 참고하면 좋을 듯 하다.

 

*이 문제를 마지막으로 해커랭크 링크드리스트 문제를 모두 풀었다. 다른 자료구조 문제들도 곧 다 풀것이다.

 

-내 풀이

    static int findMergeNode(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
        SinglyLinkedListNode temp = head1;
        while(true){
            SinglyLinkedListNode temp2 = head2;
            while(temp2!=null){
                if(temp==temp2) return temp.data;
                temp2=temp2.next;
            }
            temp=temp.next;
        }
    }

 

-좋은 풀이 : chauchi.tistory.com/entry/HackerRank-Find-Merge-Point-of-Two-Lists

 

[HackerRank] Find Merge Point of Two Lists

https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/problem Find Merge Point of Two Lists | HackerRank Given two linked lists, find the node where they merge into o..

chauchi.tistory.com