# HackerRank Find Merge Point of Two Lists problem solution

In this HackerRank Find Merge Point of Two Lists Interview preparation kit problem, You have Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge.

## Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

def __init__(self, node_data):
self.data = node_data
self.next = None

def __init__(self):
self.tail = None

def insert_node(self, node_data):

else:
self.tail.next = node

self.tail = node

while node:
fptr.write(str(node.data))

node = node.next

if node:
fptr.write(sep)

# Complete the findMergeNode function below.

#
#
#     int data
#
#

while node:
node = node.next

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

tests = int(input())

for tests_itr in range(tests):
index = int(input())

llist1_count = int(input())

for _ in range(llist1_count):
llist1_item = int(input())
llist1.insert_node(llist1_item)

llist2_count = int(input())

for _ in range(llist2_count):
llist2_item = int(input())
llist2.insert_node(llist2_item)

for i in range(llist1_count):
if i < index:
ptr1 = ptr1.next

for i in range(llist2_count):
if i != llist2_count-1:
ptr2 = ptr2.next

ptr2.next = ptr1

fptr.write(str(result) + '\n')

fptr.close()

## Problem solution in Java Programming.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

public int data;

this.data = nodeData;
this.next = null;
}
}

this.tail = null;
}

public void insertNode(int nodeData) {

} else {
this.tail.next = node;
}

this.tail = node;
}
}

public static void printSinglyLinkedList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
while (node != null) {
bufferedWriter.write(String.valueOf(node.data));

node = node.next;

if (node != null) {
bufferedWriter.write(sep);
}
}
}

// Complete the findMergeNode function below.

/*
*
*     int data;
* }
*
*/

while(temp1!=null){
temp1=temp1.next;
}
while(temp2!=null){
if(list.contains(temp2)){
break;
}
temp2=temp2.next;
}
return temp2.data;

}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int tests = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int testsItr = 0; testsItr < tests; testsItr++) {
int index = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int llist1Count = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < llist1Count; i++) {
int llist1Item = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

llist1.insertNode(llist1Item);
}

int llist2Count = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < llist2Count; i++) {
int llist2Item = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

llist2.insertNode(llist2Item);
}

for (int i = 0; i < llist1Count; i++) {
if (i < index) {
ptr1 = ptr1.next;
}
}

for (int i = 0; i < llist2Count; i++) {
if (i != llist2Count-1) {
ptr2 = ptr2.next;
}
}

ptr2.next = ptr1;

bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}

bufferedWriter.close();

scanner.close();
}
}

### Problem solution in C++ programming.

#include <bits/stdc++.h>

using namespace std;

public:
int data;

this->data = node_data;
this->next = nullptr;
}
};

public:

this->tail = nullptr;
}

void insert_node(int node_data) {

} else {
this->tail->next = node;
}

this->tail = node;
}
};

while (node) {
fout << node->data;

node = node->next;

if (node) {
fout << sep;
}
}
}

while (node) {
node = node->next;

free(temp);
}
}

// Complete the findMergeNode function below.

/*
*
*     int data;
* };
*
*/
}

}
}
return 0;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

int tests;
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
int index;
cin >> index;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

int llist1_count;
cin >> llist1_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist1_count; i++) {
int llist1_item;
cin >> llist1_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist1->insert_node(llist1_item);
}

int llist2_count;
cin >> llist2_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

for (int i = 0; i < llist2_count; i++) {
int llist2_item;
cin >> llist2_item;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

llist2->insert_node(llist2_item);
}

for (int i = 0; i < llist1_count; i++) {
if (i < index) {
ptr1 = ptr1->next;
}
}

for (int i = 0; i < llist2_count; i++) {
if (i != llist2_count-1) {
ptr2 = ptr2->next;
}
}

ptr2->next = ptr1;

fout << result << "\n";
}

fout.close();

return 0;
}

### Problem solution in C programming.

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int data;
};

};

node->data = node_data;
node->next = NULL;

return node;
}

} else {
}

}

while (node) {
fprintf(fptr, "%d", node->data);

node = node->next;

if (node) {
fprintf(fptr, "%s", sep);
}
}
}

while (node) {
node = node->next;

free(temp);
}
}

// Complete the findMergeNode function below.

/*
*
*     int data;
* };
*
*/
}

}
}
return 0;
}

int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

char* tests_endptr;
int tests = strtol(tests_str, &tests_endptr, 10);

if (tests_endptr == tests_str || *tests_endptr != '\0') { exit(EXIT_FAILURE); }

for (int tests_itr = 0; tests_itr < tests; tests_itr++) {
char* index_endptr;
int index = strtol(index_str, &index_endptr, 10);

if (index_endptr == index_str || *index_endptr != '\0') { exit(EXIT_FAILURE); }

llist1->tail = NULL;

char* llist1_count_endptr;
int llist1_count = strtol(llist1_count_str, &llist1_count_endptr, 10);

if (llist1_count_endptr == llist1_count_str || *llist1_count_endptr != '\0') { exit(EXIT_FAILURE); }

for (int i = 0; i < llist1_count; i++) {
char* llist1_item_endptr;
int llist1_item = strtol(llist1_item_str, &llist1_item_endptr, 10);

if (llist1_item_endptr == llist1_item_str || *llist1_item_endptr != '\0') { exit(EXIT_FAILURE); }

}

llist2->tail = NULL;

char* llist2_count_endptr;
int llist2_count = strtol(llist2_count_str, &llist2_count_endptr, 10);

if (llist2_count_endptr == llist2_count_str || *llist2_count_endptr != '\0') { exit(EXIT_FAILURE); }

for (int i = 0; i < llist2_count; i++) {
char* llist2_item_endptr;
int llist2_item = strtol(llist2_item_str, &llist2_item_endptr, 10);

if (llist2_item_endptr == llist2_item_str || *llist2_item_endptr != '\0') { exit(EXIT_FAILURE); }

}

for (int i = 0; i < llist1_count; i++) {
if (i < index) {
ptr1 = ptr1->next;
}
}

for (int i = 0; i < llist2_count; i++) {
if (i != llist2_count-1) {
ptr2 = ptr2->next;
}
}

ptr2->next = ptr1;

fprintf(fptr, "%d\n", result);
}

fclose(fptr);

return 0;
}

size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);

while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);

if (!line) { break; }

data_length += strlen(cursor);

if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

size_t new_length = alloc_length << 1;
data = realloc(data, new_length);

if (!data) { break; }

alloc_length = new_length;
}

if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}

data = realloc(data, data_length);

return data;
}

### Problem solution in JavaScript programming.

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});

process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*\$/, '')
.split('\n')
.map(str => str.replace(/\s*\$/, ''));

main();
});

return inputString[currentLine++];
}

constructor(nodeData) {
this.data = nodeData;
this.next = null;
}
};

constructor() {
this.tail = null;
}

insertNode(nodeData) {

} else {
this.tail.next = node;
}

this.tail = node;
}
};

while (node != null) {
ws.write(String(node.data));

node = node.next;

if (node != null) {
ws.write(sep);
}
}
}

/*
Find merge point of two linked lists
Note that the head may be 'null' for the empty list.
Node is defined as
var Node = function(data) {
this.data = data;
this.next = null;
}
*/

// This is a "method-only" submission.
// You only need to complete this method.

function findMergeNode(a, b) {
let arr = [];

while (a || b) {
if (a && arr.find(n => n === a)) return a.data;
else if(a) {
arr.push(a)
a = a.next;
}
if (b && arr.find(n => n === b)) return b.data;
else if (b) {
arr.push(b);
b = b.next;
}
}

return 0;
}

function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

for (let testsItr = 0; testsItr < tests; testsItr++) {

for (let i = 0; i < llist1Count; i++) {
llist1.insertNode(llist1Item);
}

for (let i = 0; i < llist2Count; i++) {
llist2.insertNode(llist2Item);
}

for (let i = 0; i < llist1Count; i++) {
if (i < index) {
ptr1 = ptr1.next;
}
}

for (let i = 0; i < llist2Count; i++) {
if (i != llist2Count-1) {
ptr2 = ptr2.next;
}
}

ptr2.next = ptr1;