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. ...
随机推荐
- Java:JUnit4使用详解
对于Junit的总是一知半解不太懂,现在认真梳理一下: 此次针对的是Junit4版本,注解也是在Junit4版本才有的,之前的版本并无注解功能.而注解开发基本上被认为是一种优秀的设计,所以我们写单元测 ...
- python selenium2 有关cookie操作实例及如何绕开验证码
1.先看一下cookie是啥 cookie是访问web时服务器记录在用户本地的一系列用户信息(比如用户登录信息),以便对用户进行识别 from selenium import webdriver im ...
- winrar 授权破解过期解决
RAR registration data Federal Agency for Education 1000000 PC usage license UID=b621cca9a84bc5deffbf ...
- 【转】iBatis.Net的SqlMap.config文件
转自:http://www.xuebuyuan.com/579671.html iBatis.Net基本的运行环境配置主要由两个文件组成,分别是SqlMap.config和Provider.con ...
- [洛谷P4091][HEOI2016/TJOI2016]求和
题目大意:给你$n(n\leqslant10^5)$,求:$$\sum\limits_{i=0}^n\sum\limits_{j=0}^i\begin{Bmatrix}i\\j\end{Bmatrix ...
- 【Learning】容斥原理
PPT在这里 https://files.cnblogs.com/files/RogerDTZ/%E5%AE%B9%E6%96%A5%E5%8E%9F%E7%90%86.pdf
- debian7编译安装tengine添加lua和ldap模块
1.安装开发环境 # aptitute update # aptitude install -y build-essential # aptitude install -y libldap2-dev ...
- 图像处理之CSC性能优化(源码)
1 CSC基本实现 根据前一篇CSC转换的文档了解到,RGB与YUV的变换公式如下: YCbCr(256 级别) 可以从8位 RGB 直接计算,计算公式如下: Y = 0.299 R + 0.587 ...
- C++:(拷贝,继承,智能指针)练习
#include <iostream> #include <string> #include <memory> #include <functional> ...
- MySQL修改端口号操作
在C盘下的program Files下找到MySQL文件夹 - my.ini配置文件有个port=3306 修改即可