【Hackerrank】Find the merge point of two joined linked lists
来源:互联网 发布:喵叔的实验室 知乎 编辑:程序博客网 时间:2024/05/18 12:31
You’re given the pointer to the head nodes of two linked lists that merge together at some node. Find the node at which this merger happens. The two head nodes will be different and neither will be NULL.
Input Format
You have to complete the int FindMergeNode(Node* headA, Node* headB)
method which takes two arguments - the heads of the linked lists. You should NOT read any input from stdin/console.
Output Format
Find the node at which both lists merge and return
the data
of that node. Do NOT print anything to stdout/console.
Sample Input
1 --> 2 --> 3 --> NULL ^ |1----- 1 --> 2 --> 3 --> NULL ^ | 1-----
Sample Output
23
Explanation
1. As shown in the Input, 2 is the merge point.
2. Similarly 3 is merging point
c++ code :
#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>using namespace std;struct Node{int data;Node* next;};/* Find merge point of two linked lists Node is defined as struct Node { int data; Node* next; }*/#include <stack>int FindMergeNode(Node *headA, Node *headB){ // Complete this function // Do not write the main method. stack<Node *> sta, stb; if(headA == NULL || headB == NULL) return 0; Node *cur = headA; while(cur != NULL) { sta.push(cur); cur = cur->next; } cur = headB; while(cur != NULL) { stb.push(cur); cur = cur->next; } int last = 0; while(!sta.empty() && !stb.empty()) { Node *pa = sta.top(); Node *pb = stb.top(); sta.pop(); stb.pop(); if(pa->data != pb->data) { break; } else last = pa->data; } return last;}int main(){Node *A, *B, *C, *D,*E,*F,*G;A = new Node();B= new Node(); C= new Node(); D = new Node(); E = new Node(); F= new Node();G = new Node();A->data = 2; B->data = 4; C->data = 3; D->data = 5; E->data = 7; F->data = 6;G->data = 11;// case 1 = A->next = B; B->next = C; C->next = D; D->next = E; E->next = NULL;F->next = G; G->next = C;cout<<FindMergeNode(A,F)<<"\n";//case 2.A->next = B; B->next = C; C->next = E; E->next = NULL;F->next = G; G->next = D;D->next = C;cout<<FindMergeNode(A,F)<<"\n";//case 3:A->next = B; B->next = E; E->next = NULL;F->next = G; G->next = D;D->next = C; C->next = E;cout<<FindMergeNode(A,F)<<"\n";}
0 0
- 【Hackerrank】Find the merge point of two joined linked lists
- 【Hackerrank】Merge two sorted linked lists
- Find the first intersection of two lists (if the two list are merged at some point)
- Merge two sorted linked lists
- Merge two sorted linked lists
- Merge two sorted linked lists
- Merge two Sorted linked lists
- Find the middle point of the linked list
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Leetcode_160_Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- 记一个语音识别框架(SpeechKit.faramework)
- 《程序员的自我修养》-链接、装载与库
- 多线程测试用户注册
- linux安装apache2.4
- Linux系统服务启动与停止
- 【Hackerrank】Find the merge point of two joined linked lists
- monit
- 网络拓扑发现
- Bug管理系统UML2.0建模实例(一)
- html title 换行
- Scroller类简介
- Mysql常用命令
- python总结
- jQuery 效果 - 隐藏和显示