C++算法之 找出两个链表的公共节点

题目:输入两个链表,找出它们第一个公共节点。链表节点定义如下:

struct ListNode

int    m_nKey;

ListNode*   m_pNext;

方法1:在第一个链表上顺序遍历每一个节点,没遍历一个节点,在第二个链表上顺序遍历每个节点。  O(n^2)

方法2:找到两个链表的长度差,先遍历长链表到短链表的长度处,然后两个链表同时遍历,没遍历依次比较两个节点指针是否相同,

注意是比较节点指针,不是节点的值!

代码:

// FindFirstCommandNode.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

struct ListNode
{
	int         m_nKey;
	ListNode*   m_pNext;

	ListNode(int i):m_nKey(i)
	{

	}
};

//获取链表长度
int GetListLength(ListNode* pHead)
{
	int nLength = 0;
	ListNode* pNode = pHead;
	while (pNode != NULL)
	{
		++nLength;
		pNode = pNode->m_pNext;
	}
	return nLength;
}

ListNode* FindFirstCommandNode(ListNode* pHead1, ListNode* pHead2)
{

	int  nLength1 = GetListLength(pHead1);
	int  nLength2 = GetListLength(pHead2);
	int nLengthDif = 0;//两个链表的长度差
	ListNode* pListHeadLong  = NULL;//用于指向长链表
	ListNode* pListHeadShort = NULL;//用于指向短链表

	//根据长度判断 链表指向
	if (nLength1 > nLength2)
	{
	    nLengthDif = nLength1 - nLength2;
		pListHeadShort = pHead2;
		pListHeadLong  = pHead1;
	}
	else
	{
		nLengthDif = nLength2 - nLength1;
		pListHeadLong  = pHead2;
		pListHeadShort = pHead1;
	}

	//先对长链表进行移动 移动到与短链表长度相同的位置
	for (int i = 0; i < nLengthDif; i++)
	{
		pListHeadLong = pListHeadLong->m_pNext;
	}
	//寻找公共节点
	while (pListHeadLong !=NULL && pListHeadShort != NULL && pListHeadLong!= pListHeadShort)
	{
		pListHeadLong  = pListHeadLong->m_pNext;
		pListHeadShort = pListHeadShort->m_pNext;
	}
	//如果不为空  此时的pListHeadLong 与pListNodeShort为同一个节点,返回该节点
	if (pListHeadLong != NULL)
	{
		return pListHeadLong;
	}
	else
	{
		return NULL;//否则返回NULL
	}
}

int _tmain(int argc, _TCHAR* argv[])
{

	ListNode* head1 = new ListNode(0);
	ListNode* head2 = new ListNode(1);
	ListNode* node0 = new ListNode(22);
	ListNode* node1 = new ListNode(2);
	ListNode* node2 = new ListNode(3);
	ListNode* node3 = new ListNode(4);
	ListNode* node4 = new ListNode(5);
	ListNode* node5 = new ListNode(6);
	ListNode* node6 = new ListNode(7);
	ListNode* node8 = new ListNode(6);

	head1->m_pNext = node1;
	node1->m_pNext = node0;
	node0->m_pNext = node3;
	node3->m_pNext = node5;
	node5->m_pNext = node6;
	node6->m_pNext = NULL;

	head2->m_pNext = node2;
	node2->m_pNext = node4;
	node4->m_pNext = node8;
	node8->m_pNext = node6;
	node6->m_pNext = NULL;

	cout<<"链表1的长度为:"<<GetListLength(head1)<<endl;
	cout<<"链表2的长度为:"<<GetListLength(head2)<<endl;

	ListNode* CommNode = FindFirstCommandNode(head1,head2);
	if (CommNode!= NULL)
	{
		cout<<"公共节点的值为:"<<CommNode->m_nKey<<endl;
	}
	else
	{
		cout<<"没有公共节点"<<endl;
	}
	getchar();
	return 0;
}

时间: 2024-08-25 15:19:15

C++算法之 找出两个链表的公共节点的相关文章

剑指offer——两个链表的公共节点

题目链接:输入两个链表,找出它们的第一个公共结点. 解题思路: 找出2个链表的长度,然后让长的先走两个链表的长度差,然后再一起走(因为2个链表用公共的尾部) 1 /* 2 public class ListNode { 3 int val; 4 ListNode next = null; 5 6 ListNode(int val) { 7 this.val = val; 8 } 9 }*/ 10 public class Solution { 11 public ListNode FindFir

[PHP] 算法-请找出带环链表的环的入口结点的PHP实现

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后移动,当第一个结点到达末尾的时候,第二个结点所在位置就是倒数第k个节点了 2.原理有点像上面的,定义两个指针,一个是快指针每次走两步,一个是慢指针每次走一步,当两个相遇的时候,假设环的长度为n个结点,慢指针走x步,快指针走2x步,2x=x+kn ;x=kn; k暂时假定为1圈 ,也就是慢指针slow

160. Intersection of Two Linked Lists(找出两个链表的交点)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

两个链表的第一个公共结点-输入两个链表,找出它们的第一个公共结点。

1.蛮力法: 1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution { 10 public: 11 ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) { 12 if(pHead1==NULL||pHead2

[算法学习]给定一个整型数组,找出两个整数为指定整数的和(3)

问题描述: 设计一个类,包含如下两个成员函数: Save(int input) 插入一个整数到一个整数集合里. Test(int target) 检查是否存在两个数和为输入值.如果存在着两个数,则返回true,否则返回false 允许整数集合中存在相同值的元素 分析: 与[算法学习]给定一个整型数组,找出两个整数为指定整数的和(2)不同,这里需要算出的是存不存在这两个数,可以在上一篇的基础上修改一下数据结构,HashMap其中key是数值,value是数值个数,然后需要作两步判断,map中存在数

输入两个链表,找出它们的第一个公共结点

import java.util.*; /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { ListNode head = pHead1; Set<ListNo

找出两个数组的相同元素,最优算法?

在做新旧接口交替过程中,遇到了老接口和新接口json数据有些不一致的情况,需要比较两个json对象,把相同的元素赋其中一个json对象中变量的值.而且其中一个json最后输出格式还需要改变下属性名,思来想去觉得和"找出两个数组相同元素"很像,所以做下总结. "有一个数组A{0,2,3,5}和一个数组B{3,5,6,2,1,1},找出这两个数组相同元素." 一开始抽象出这道题时,脑海里浮现出最简单粗暴的方法,逐一比较. //最简单粗暴的做法,逐个比较,时间复杂度为(B

有两个变量a和b,不用“if”、“? :”、“switch”或其他判断语句,找出两个数中比较大的

1.问题 There are two int variables: a and b, don't use "if"."? :"."switch" or other judgement statement, find out the biggest one of the two numbers. (有两个变量a和b,不用"if"."? :"."switch"或其他判断语句,找出两个数中比较

找出两个文本文件的不同的行

用shell找出两个文本文件的不同的行 亲自实验过的方法如下: 第一种:comm命令法 命令如下:comm -3 file1 file2 有一个问题就是,如果两个文件排序不一样的话,会出问题 第二种:grep命令法 命令如下:grep -vwf file1 file2 统计file1中没有,file2中有的行 具体使用环境以后再补充,今天先记录到这里. 参考文档: 1.找出两个文件内容的相同与不同:http://blog.csdn.net/shuckstark/article/details/7