求两个链表的第一个公共节点

链表A:1->2->3->6->7
链表B:4->5->6->7

第一个公共节点:6
在这里插入图片描述

#include "pch.h"
#include<iostream>
#include<stack>
using namespace std;
struct NODE
{
	int m_value;
	struct NODE *next;
};

/*
*创建头结点
*/
NODE* Linklist_init(void)
{
	NODE *p = new NODE;

	p->m_value = 0;
	p->next = NULL;

	return p;
}
/*
*头插法插入节点
*/
void insertnode(NODE *P, int value)
{	
	NODE *Q = new NODE;
	Q->m_value = value;

	Q->next = P->next;
	P->next = Q;
}

/*创建两个有相交节点的链表*/
void creat(NODE *PA, NODE *PB)
{
	int index = 0;
	NODE *tempa1 = NULL, *tempa2 = NULL;
	NODE *tempb1 = NULL, *tempb2 = NULL;


	for (index = 3; index >= 1; index--)
	{
		insertnode(PA, index);
	}
	
	for (index = 5; index > 3; index--)
	{
		insertnode(PB, index);
	}

	for (NODE *tempa1 = PA; tempa1->next != NULL; tempa1 = tempa1->next)
	{
		tempa2 = tempa1->next;
	}
	for (NODE *tempb1 = PB; tempb1->next != NULL; tempb1 = tempb1->next)
	{
		tempb2 = tempb1->next;
	}
#if 1//创建共同的节点
	for (index = 6; index < 8; index++)
	{
		NODE *Q = new NODE;
		Q->m_value = index;

		tempa2->next = tempb2->next = Q;
		Q->next = NULL;

		tempa2 = tempb2 = Q;
	}
#endif
}

void show(NODE *P)
{
	if (P->next == NULL)
	{
		return;
	}
	for (NODE *temp = P->next; temp != NULL; temp = temp->next)
	{
		printf("%d\n", temp->m_value);
	}
	return;
}


int linklistlength(NODE *P)
{
	int length = 0;
	if (P->next == NULL)
	{
		return 0;
	}

	while (P->next)
	{
		P = P->next;
		length++;
	}
	return length;
}


NODE *P(NODE *Q, NODE *R)
{
	int length1, length2, diff;
	if (Q->next == NULL || R->next == NULL)
	{
		return NULL;
	}

	length1 = linklistlength(Q);
	length2 = linklistlength(R);

	diff = length1 - length2;
	NODE *lengthlong = Q;
	NODE *lengthshort = R;


	if (length1 < length2)
	{
		NODE *lengthlong = R;
		NODE *lengthshort = Q;

		diff = length2 - length1;
	}

	for (int index = 0; index < diff; index++)
	{
		lengthlong = lengthlong->next;
	}

	while ((lengthlong != NULL) && (lengthshort != NULL)
		&& (lengthlong != lengthshort))
	{
		lengthlong = lengthlong->next;
		lengthshort = lengthshort->next;
	}

	NODE *firstCommonNode = lengthlong;
	return firstCommonNode;
}


int main(int argc, char **argv)
{
	//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF);//查看内存泄漏

	NODE *PA = Linklist_init();
	NODE *PB = Linklist_init();

	creat(PA, PB);

	cout << "查看链表A的节点" << endl;
	show(PA);
	cout << "查看链表B的节点" << endl;
	show(PB);


	//第一个公共节点
	NODE *first = P(PA, PB);
	cout << first->m_value << endl;

	//_CrtDumpMemoryLeaks();/*查看内存泄漏*/
	return 0;
}

在这里插入图片描述
解题方法:
1:暴力法,
当循环一个链表的节点时,遍历另一个链表
2:将两个链表拼接起来。 将两个链表进行拼接,一个链表1在前链表2在后,另一个链表2在前链表1在后,则合成的两个链表一样长,然后同时遍历两个链表,就可以找到公共结点。
3:我们可以把两个链表的结点依次压入到两个辅助栈中,这样两个链表的尾结点就位于两个栈的栈顶,接下来比较两个栈顶的结点是否相同。如果相同,则把栈顶弹出继续比较下一个,直到找到最后一个相同的结点。
4:先求出两个链表各自的长度,然后将长的链表的头砍掉,也就是长的链表先走几步,使得剩余的长度与短链表一样长,这样同时向前遍历便可以得到公共结点

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值