Programming/Data Structure
[Hackerrank] FindMergePointofTwoLists
읽고 쓰는 개발자
2020. 12. 2. 22:29
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;
}
}