单向列表求交点_Y型链接列表中的交点

单向列表求交点

Problem statement:

问题陈述:

There are two singly linked lists where end nodes of one linked list got linked into the second list, forming aY (or 'T') shaped list. Write a program to get the point where two linked lists got merged.

有两个单链接列表,其中一个链接列表的末端节点链接到第二个列表,形成一个Y(或“ T”)形状的列表。 编写程序以使两个链接列表合并。

Example:

例:

    1 → 2 → 3 → 6 ← 4 ← 5
                ↓
                8
                ↓
               NULL

head1 is 1 and head2 is 5, both have a common merged tail portion which is 6 -> 8 -> NULL. So the intersection point is 6.

头像1是1和HEAD2是5,两者具有共同的合并尾部是6 - > 8 - > NULL。 因此, 交点为6

Solution:

解:

Pre-requisite:

先决条件:

  1. Set s to store linked list nodes.

    集合S存放链表节点。

  2. Two head pointers pointing to the Y('T') shaped linked list.

    两个指向Y('T')形链表的头指针。

Algorithm:

算法:

1.  Declare set 's' to store nodes of the linked list;
2.  Declare and define two pointer to node type variable 
    Node* temp1=head1,*temp2=head2 //for traversing

3.  While(temp1){ //until the first linked list is traversed
        Insert all the nodes (pointer to nodes of course) to the set; 
        //remember nodes to be entered not the data value
        Insert(s, temp1);
        temp1=temp1->next;
    END While
4.  While(temp2)
        IF (if temp2 is not present in the set) //not part of merged tail
            temp2=temp2->next;	
        Else
            return temp2->data; //first intersection point found
        End IF-ELSE
    End While

Note:

注意:

The nodes (pointer to nodes) are to be inserted into the set, not the node data. Let's say for an input example both heads have the same input data (for example say 1), but nodes are actually different (no intersection there). But if we maintain the set with node data, then that will return the 1, which is the wrong answer.

节点(指向节点的指针)要插入到集合中,而不是节点数据中。 假设对于一个输入示例,两个头具有相同的输入数据(例如说1),但是节点实际上是不同的(那里没有交集)。 但是,如果我们用节点数据维护该集合,则将返回1,这是错误的答案。

    1 → 2 → 3 → 6 ← 4 ← 1
                ↓
                8
                ↓
               NULL

For the above example, answer will be 1 if we start inserting node->data to the set instead of node. But the actual answer is 6.

对于上面的示例,如果我们开始将node-> data而不是node插入到集合中,则答案将为1。 但是实际答案是6。

Explanation with example:

举例说明:

    The above algorithm is pretty simple and self-explanatory.
    Discussion for above example:
    After processing the first list the set contains:
    Pointer to Node having value 1
    Pointer Node having value 2
    Pointer to Node having value 3
    Pointer Node having value 6
    Pointer Node having value 8
    NULL
    When second list is processed and checked,
    Pointer to node having value 1 is not in set (any doubt!Why? think yourself)
    Pointer to node having value 4 is not in set (No doubt)
    Pointer to node having value 6 is in the set 
    (then what’s the difference between the first case where you have doubt!!! 
    Reason is different address of the nodes though same value since those are 
    different node, but in the second case the common node is same node)
    Thus it returns 6.


C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

class Node{
	public:
	int data; // data field
	Node *next;//next pointer
};

Node* creatnode(int d){
	Node* temp=(Node*)malloc(sizeof(Node));
	temp->data=d;
	temp->next=NULL;
	return temp;
}

int intersectPoint(Node* head1, Node* head2)
{
	set<Node*> s;
	Node* temp1=head1,*temp2=head2;
	
	while(temp1){
		s.insert(temp1);
		temp1=temp1->next;
	}
	while(temp2){
		if(s.find(temp2)==s.end()){
			temp2=temp2->next;
		}
		else{
			return temp2->data;
		}
	}
	return -1;//not at all a Y-shaped one
}

int main(){
	cout<<"Linked list built like modified example\n";
	
	Node* head1=creatnode(1);
	Node* head2=creatnode(1);
	
	head1->next=creatnode(2);
	head2->next=creatnode(4);
	head1->next->next=creatnode(3);
	head1->next->next->next=creatnode(6);
	head2->next->next=head1->next->next->next;//merged
	head1->next->next->next->next=creatnode(8);

	if(intersectPoint(head1,head2)==-1)
		cout<<"Not Y/T shapped at all";
	else{
		cout<<"intersection point is:";
		cout<<intersectPoint(head1,head2)<<endl;
	}
	
	return 0;
}

Output

输出量

Linked list built like modified example
intersection point is:6 


翻译自: https://www.includehelp.com/icp/intersection-point-in-y-shaped-linked-list.aspx

单向列表求交点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值