前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

作者头像
Enjoy233
发布2019-03-05 14:42:18
3290
发布2019-03-05 14:42:18
举报

剑指offer之面试题37 两个链表的第一个公共结点

提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tqId=11189

leetcode 160: https://leetcode.com/problems/intersection-of-two-linked-lists/

参与人数:3252   时间限制:1秒   空间限制:32768K

本题知识点: 链表 时间空间效率的平衡 题目描述 输入两个链表,找出它们的第一个公共结点。

分析:

方法1:

使用两个辅助栈,从尾部往头部找最后一个共同节点。但这种方法空间复杂度较高,时间复杂度为O(m+n)。

方法2:

先分别遍历两个链表,取各自的长度。较长的链表中的头指针一直往后面走,直到遇到相同节点。时间复杂度为O(m+n)。

代码语言:javascript
复制
#include <iostream>
using namespace std;

struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
};
 
class Solution {
public:
	int getLen(ListNode *head)
	{
		int count=0;
		ListNode *p=head;
		while(p != NULL)
		{
			count++;
			p=p->next;
		}
		return count;		
	}
	
    ListNode* FindFirstCommonNode(ListNode *pHead1, ListNode *pHead2) {
        if(pHead1==NULL || pHead2==NULL) return NULL;
        int len1=getLen(pHead1);
        int len2=getLen(pHead2);
        
        if(len1 >= len2)
        {
        	int gap=len1-len2;
        	while(gap--) pHead1=pHead1->next;
		}
		else {
        	int gap=len2-len1;
        	while(gap--) pHead2=pHead2->next;			
		}
		while(pHead1 != pHead2)
		{
			pHead1=pHead1->next;
			pHead2=pHead2->next;
		}
        return pHead1;
    }
};

// 以下为测试部分
int main()
{
	ListNode *pA=new ListNode(1);
	ListNode *p1=new ListNode(2);
	ListNode *p2=new ListNode(3);
	ListNode *pB=new ListNode(4);	
	ListNode *p3=new ListNode(5);		
	ListNode *p4=new ListNode(6);
	ListNode *p5=new ListNode(7);
	
	pA->next=p1;
	p1->next=p2;
	p2->next=p4;
	pB->next=p3;
	p3->next=p4;
	p4->next=p5;
	p5->next=NULL;	

	Solution sol;
	
	ListNode *pOut=sol.FindFirstCommonNode(pA,pB);
	
	cout<<pOut->val<<endl;
	return 0;
}

LeetCode 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:

代码语言:javascript
复制
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.
代码语言:javascript
复制
#include <iostream>
using namespace std;
struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
}; 
class Solution {
public:
	int getLen(ListNode *head)
	{
		int count=0;
		ListNode *p=head;
		while(p != NULL)
		{
			count++;
			p=p->next;
		}
		return count;		
	}	
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {

        if(headA==NULL || headB==NULL) return NULL;
        int len1=getLen(headA);
        int len2=getLen(headB);
        
        if(len1 >= len2)
        {
        	int gap=len1-len2;
        	while(gap--) headA=headA->next;
		}
		else {
        	int gap=len2-len1;
        	while(gap--) headB=headB->next;			
		}
		while(headA != headB)
		{
			headA=headA->next;
			headB=headB->next;
		}
        return headA;
    }
};

// 以下为测试部分
int main()
{
	ListNode *pA=new ListNode(1);
	ListNode *p1=new ListNode(2);
	ListNode *p2=new ListNode(3);
	ListNode *pB=new ListNode(4);	
	ListNode *p3=new ListNode(5);		
	ListNode *p4=new ListNode(6);
	ListNode *p5=new ListNode(7);	
	pA->next=p1;
	p1->next=p2;
	p2->next=p4;
	pB->next=p3;
	p3->next=p4;
	p4->next=p5;
	p5->next=NULL;
	Solution sol;	
	ListNode *pOut=sol.getIntersectionNode(pA,pB);	
	cout<<pOut->val<<endl;
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年07月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档