码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_160_Intersection of Two Linked Lists

时间:2015-02-13 11:42:29      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   linked_list   

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢技术分享


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:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
begin to intersect at node c1.

Notes:

If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.


//先判断是否相交,如果相交,那么两个链表最后的节点是一样的。
//然后,长的链表开始多走 (A的数量 - B的数量)步,然后和短链表同步往下走,遇到的第一个相同的节点就是最早的公共节点
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if( headA==NULL || headB==NULL ) 
			return NULL;
		int countA=0 , countB=0 , countC=0;
		ListNode *tempA = headA;
		ListNode *tempB = headB;
		while(tempA)
		{
			tempA = tempA->next;
			countA++;
		}
		while(tempB)
		{
			tempB = tempB->next;
			countB++;
		}
		if(tempA!=tempB)
			return NULL;

		tempA = headA;
		tempB = headB;
		if(countA<=countB)
		{
			countC = countB-countA;
			while(countC)
			{
				tempB= tempB->next;
				countC--;
			}
		}
		else
		{
			countC = countA-countB;
			while(countC)
			{
				tempA= tempA->next;
				countC--;
			}
		}
		while( tempA!=tempB)
		{
			tempA= tempA->next; 
			tempB= tempB->next;
		}
		return tempA;
    }
};



//先判断是否相交,如果相交,那么两个链表最后的节点是一样的。
//然后,长的链表开始多走 (A的数量 - B的数量)步,然后和短链表同步往下走,遇到的第一个相同的节点就是最早的公共节点
#include<iostream>

using namespace std;

#define N 5

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if( headA==NULL || headB==NULL ) 
			return NULL;
		int countA=0 , countB=0 , countC=0;
		ListNode *tempA = headA;
		ListNode *tempB = headB;
		while(tempA)
		{
			tempA = tempA->next;
			countA++;
		}
		while(tempB)
		{
			tempB = tempB->next;
			countB++;
		}
		if(tempA!=tempB)
			return NULL;

		tempA = headA;
		tempB = headB;
		if(countA<=countB)
		{
			countC = countB-countA;
			while(countC)
			{
				tempB= tempB->next;
				countC--;
			}
		}
		else
		{
			countC = countA-countB;
			while(countC)
			{
				tempA= tempA->next;
				countC--;
			}
		}
		while( tempA!=tempB)
		{
			tempA= tempA->next; 
			tempB= tempB->next;
		}
		return tempA;
    }
};

ListNode *creatlist()
{
	ListNode *head;
	head=NULL;

	for(int i=0; i<N; i++)
	{
		int a;
		ListNode *p;
		cin>>a;
		p = (ListNode*)malloc(sizeof(ListNode));
		p->val=a;
		p->next=head;
		head = p;
	}
	return head;
}

int main()
{
	ListNode *list1=creatlist();
	ListNode *list2=creatlist();
	Solution lin;
	cout<<lin.getIntersectionNode( list1,list2 )->val<<endl;
}


leetcode_160_Intersection of Two Linked Lists

标签:c++   leetcode   linked_list   

原文地址:http://blog.csdn.net/keyyuanxin/article/details/43791931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!