2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists
本题收获:
1.链表的输入输出
2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可。
题目: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.
注意:题目中两个链表是有交叉点,只要a2,b3指向的下一个节点地址都是c1的即可。
思路:
我的思路:刚开把题目理解错了,以为是两个单链表有相同的节点。
leetcode/dicuss思路:看看网上的思路
那题目的最好解法,这技巧问题阿,遍历list1 后接着遍历list2,同时,遍历list2然后遍历list1,这样两个遍历的长度是一样的O(n+m),怎么判断相等呢?
- 判断list 是否有NULL 情况
- 同时遍历 两个新链表
- 如果节点地址相同,返回
- 如果不相同继续遍历
- 遍历结束返回NULL
代码:
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *p1 = headA, *p2 = headB;
if(p1 == NULL || p2 == NULL)
return NULL;
while(p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
if(p1 == NULL && p2 != NULL)
p1 = headB;
if(p2 == NULL && p1 != NULL)
p2 = headA;
}
return p1;
}
};
我的测试代码:
代码1:是错误的理解,即两个单独的链表。
#include "stdafx.h"
#include "iostream"
#include "vector"
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL){} //what this mean
}; class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *p1 = headA;
ListNode *p2 = headB; if (p1 == NULL || p2 == NULL) return NULL; while (p1 != NULL && p2 != NULL && p1 != p2) {
p1 = p1->next;
p2 = p2->next;
if (p1 == NULL) p1 = headB->next;
if (p2 == NULL) p2 = headA->next;
//cout << p1->val << endl;
//cur1 = cur1->next;
//cout << p2->val << endl;
//cur2 = cur2->next;
//cout << cur2->val << endl;
// Any time they collide or reach end together without colliding
// then return any one of the pointers.
//
if (p1->val == p2->val) //如果是两个链表找相同点,则(p1->val ==p2->val)
{
//cout << p1 << endl;
return p1;
}
//
// If one of them reaches the end earlier then reuse it
// by moving it to the beginning of other list.
// Once both of them go through reassigning,
// they will be equidistant from the collision point.
// if (p1 == NULL && p2 == NULL)
{
//cur2 = headA;
//cout << "hhh" << endl;
}
}
//cout << p1 << endl;
return p1;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
ListNode *h1 = new ListNode(), *h2 = new ListNode(), *p1 = NULL, *node1 = NULL, *p2 = NULL, *node2 = NULL, *head = NULL;
vector<int> nums1 = { , , , }, nums2 = { , , , , };
//nums1 = { 1, 2, 3, 4 }; //题目理解错误,条件给的出错,
//nums2 = { 0, 9, 7, 3, 4 };
p1 = h1;
p2 = h2;
for (size_t i = ; i < nums1.size(); i++)
{
node1 = new ListNode(nums1.at(i)); if (h1 == NULL)
{
h1 = node1;
}
else p1->next = node1;
p1 = node1;
} for (size_t j = ; j < nums2.size(); j++)
{
node2 = new ListNode(nums2.at(j));
if (h2 == NULL)
{
h2 = node2;
}
else p2->next = node2;
p2 = node2;
} Solution solution;
head = solution.getIntersectionNode(h1, h2);
cout << head->val << endl; //为空所以出错
system("pause");
return ;
}
代码2:正确思路代码,也可以看到这两的mian函数的区别。
#include "stdafx.h"
#include "iostream"
#include "vector"
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL){} //what this mean
}; /*class MyClass
{
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
{
ListNode *cur1 = headA, *cur2 = headB;
//cur1 = headA, cur1 = headB;
//cout << cur1 << endl;
if (cur1 == NULL || cur2 == NULL)
return NULL; while (cur1 != cur2)
{
//cout << cur1 << endl;
cur1 = cur1->next;
//cout << cur1->val << endl;
cur2 = cur2->next;
//cout << cur2->val << endl;
if (cur1 == NULL && cur2 != NULL)
{
//cout << cur1 << endl;
cur1 = headB;
//cout << cur1->val << endl;
}
if (cur2 == NULL && cur1 != NULL)
{
cur2 = headA;
//cout << cur2->val << endl;
}
if (cur2 == NULL && cur1 == NULL) //测试是否进入空指针
{
//cur2 = headA;
cout << "hhh" << endl;
}
}
return cur1;
//cout << cur1->val << endl;
}
};*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *p1 = headA;
ListNode *p2 = headB; if (p1 == NULL || p2 == NULL) return NULL; while (p1 != NULL && p2 != NULL && p1 != p2) {
p1 = p1->next;
p2 = p2->next;
if (p1 == NULL) p1 = headB->next;
if (p2 == NULL) p2 = headA->next;
//cout << p1->val << endl;
//cur1 = cur1->next;
//cout << p2->val << endl;
//cur2 = cur2->next;
//cout << cur2->val << endl;
// Any time they collide or reach end together without colliding
// then return any one of the pointers.
//
if (p1 == p2) //如果是两个链表找相同点,则(p1->val ==p2->val)
{
//cout << p1 << endl;
return p1;
}
//
// If one of them reaches the end earlier then reuse it
// by moving it to the beginning of other list.
// Once both of them go through reassigning,
// they will be equidistant from the collision point.
// if (p1 == NULL && p2 == NULL)
{
//cur2 = headA;
//cout << "hhh" << endl;
}
}
//cout << p1 << endl;
return p1;
}
}; int main()
{
ListNode *head1, *head2, *node1, *node2, *node3,*node4,*node5,*head;
node1 = new ListNode(); //这样的赋值,链表就会有交叉点了,和题目一样
node2 = new ListNode();
node3 = new ListNode();
node4 = new ListNode();
node5 = new ListNode(); head1 = node1;
head1->next = node2;
node2->next = node5;
node5->next = NULL; head2 = node3;
head2->next = node4;
node4->next = node5;
node5->next = NULL; Solution solution;
head = solution.getIntersectionNode(head1, head2);
cout << head->val << endl;
system("pause");
return ;
}
可以参见http://www.cnblogs.com/Azhu/p/4149738.html
2016.5.24——Intersection of Two Linked Lists的更多相关文章
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ex ...
- LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 【leetcode】Intersection of Two Linked Lists
题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
随机推荐
- MySql的多存储引擎架构, 默认的引擎InnoDB与 MYISAM的区别(滴滴)
1.存储引擎是什么? MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术, ...
- delphi开发学习四:TClientDataSet与TDataSetProvider控件使用实例
1.TClientDataSet控件 通过TClientDataSet控件可以建立瘦客户端的应用程序,且数据执行效率较高,但它不能和数据库自动连接,程序中必须制定它如何获取数据.一般情况下,TClie ...
- UpdateBatch到底是怎么用的?
要使用ADOQuery的UpdateBatch函数,必须将ADOQuery的LockType属性设置成ltBatchOptimistic
- Cannot create file"C:\Users\LML\AppData\Local\Temp\EditorLineEnds.ttr"。另一个程序正在使用此文件,进程无法访问。
不能二次启动,每次开机第一次都ok,出于习惯,总是想试试第二次打开软件是否正常,结果不出所料,出现了“Cannot create file"C:\Users\LML\AppData\Loca ...
- POJ1815_Friendship
一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案. 求最小点割,先拆点,容量为1,普通边容量无穷,最大流即为应删点数. 需要求出字典序最小的方案,可以从小到大枚举所 ...
- Java IO流学习总结 - BIO
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据 ...
- c++11 继承控制:final和override
c++11 继承控制:final和override #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <str ...
- C++11并发编程个人小结
thread_local变量在每个线程第一次执行到时初始化(类似static),并在每个线程各自累加,并在线程结束时释放. std::condition_variable:: wait(std::un ...
- Nginx 线上配置实例
1 /etc/nginx/nginx.conf,在主配置下设置 /etc/nginx/conf.d/*.conf user nginx;worker_processes 1; error_log /v ...
- Web前端开发神器--WebStorm(JavaScript 开发工具) 8.0.3 中文汉化破解版
WebStorm(JavaScript 开发工具) 8.0.3 中文汉化破解版 http://www.jb51.net/softs/171905.html WebStorm 是jetbrains公司旗 ...