본문 바로가기
Programming/Data Structure

[Hackerrank] FindMergePointofTwoLists

by 읽고 쓰는 개발자 2020. 12. 2.

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

package exam.complete;

//https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists/problem


import exam.SinglyLinkedListNode;

public class FindMergePointofTwoLists {
    static int findMergeNode(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {
//        Find the duplicate node
//        nested loop
        SinglyLinkedListNode head2NodeForLoop;
        while (head1 != null) {
            head2NodeForLoop = head2;
            while (head2NodeForLoop != null) {
                if (head1 == head2NodeForLoop) {
                    return head1.data;
                }
                head2NodeForLoop = head2NodeForLoop.next;
            }

            head1 = head1.next;
        }
        return 0;
    }
}

'Programming > Data Structure' 카테고리의 다른 글

[Hackerrank] InsertNodeAtHead  (0) 2020.12.02
[Hackerrank] GetNodeValue  (0) 2020.12.02
[Hackerrank] DeleteNode  (0) 2020.12.02
[Hackerrank] CompareTwoLinkedLists  (0) 2020.12.02
[Hackerrank] BalancedBrackets  (0) 2020.12.02