C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指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)。
#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:
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.
- If the two linked lists have no intersection at all, return
#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;
}
C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告的更多相关文章
- 剑指Offer - 九度1505 - 两个链表的第一个公共结点
剑指Offer - 九度1505 - 两个链表的第一个公共结点2013-11-24 20:09 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例.对于每个测试案例 ...
- 剑指offer 面试题52. 两个链表的第一个公共节点
这题之前leetcode做过,权当复习 首先这题没说是否一定有公共节点,如果代码可能因为这一点造成死循环的,需要提前验证所给两个链表是否有公共节点. 方法1:对于每一个list1的节点,遍历list2 ...
- 【剑指Offer】36、两个链表的第一个公共结点
题目描述: 输入两个链表,找出它们的第一个公共结点. 解题思路: 本题首先可以很直观的想到蛮力法,即对链表1(假设长度为m)的每一个结点,遍历链表2(假设长度为n),找有没有与其相同的 ...
- 【剑指offer】面试题 52. 两个链表的第一个公共结点
面试题 52. 两个链表的第一个公共结点 NowCoder 题目描述 输入两个链表,找出它们的第一个公共结点. Java 实现 ListNode Class class ListNode { int ...
- 【Offer】[52] 【两个链表的第一个公共结点】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个链表,找出它们的第一个公共结点.下图中6为公共结点:  牛客网刷题地址 思路分析 如果两个链表有公共节点,那么公共节点出现在两 ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- 剑指Offer(三十六):两个链表的第一个公共结点
剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 【剑指Offer面试编程题】题目1505:两个链表的第一个公共结点--九度OJ
题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为两个整数m和n(1<=m,n<=1000):代表将要输入的两个链表 ...
- 《剑指offer》第五十二题(两个链表的第一个公共结点)
// 面试题52:两个链表的第一个公共结点 // 题目:输入两个链表,找出它们的第一个公共结点. #include <iostream> #include "List.h&quo ...
随机推荐
- in 索引失效的问题
先安利一篇博文MySQL的or/in/union与索引优化 简单的in查询 索引失效: 步骤 1.检查建立索引没有 order_status 字段为普通索引的tinyint类型 2.检查是否使用了使索 ...
- base加密解密工具类
public class MLDUtil { public static Key DEFAULT_KEY = null; public static final String DEFAULT_SECR ...
- 虚拟机上的Ubuntu 文件系统成为只读模式的解决办法
虚拟机环境的Linux系统由于是虚拟化虚拟出来的主机环境,因此 经常会出现一些操作系统的问题,今天我遇到了一个Ubuntu操作系统文件系统成了只读模式,无法进行系统的操作,由于出问题的主机是我个人搭建 ...
- console.log
其实,console.log 不仅仅有一下应用 console.log() 这个应该是最常用的 console.error() 输出错误信息 会以红色显示 console.assert(bool,”i ...
- 190327 Python登录接口
#!Author:John # _*_ coding: utf-8 _*_ #编写登录接口 #输入用户名密码 #认证成功后显示欢迎信息 #输错三次后锁定 import sys, os, getpass ...
- 自定义类在PropertyGrid上的展示方法
自定义类在PropertyGrid上的展示方法 零.引言 PropertyGrid用来显示某一对象的属性,但是并不是所有的属性都能编辑,基本数据类型(int, double等)和.Net一些封装的类型 ...
- 初学angular项目中遇到的一些问题
1.当angular渲染完成后操作DOM树方法 //当数据渲染完毕 ngApp.directive('repeatFinish', function () { return { ...
- python--ModuleFoundError
python 模块导入错误: 1. 首先py文件名不能和导入的模块名相同 (我在学习matplotlib库的时候就把文件名设置成matplotlib 多次运行不成功) 2. 由于我电脑上只有numpy ...
- spring事务管理方式,aop
达内12 note unit 09 01 1.spring事务管理 2.spring提供了对事务管理支持 spring采用aop机制完成事务控制 可以实现在不修改原有组件代码情况下实现事务控制功能. ...
- 关于实体类getset方法首字母小写问题
实体类:private Date cDateTime;private String cNickname; public Date getcDateTime() { return cDateTime;} ...