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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
算法:
1)把其中的一条链首尾相连,组成一个环,这样问题就转化成:判断一条链表是否有环,如果有,求环的入口问题。
2)把两个链表都首尾颠倒顺序,然后比较。比较完之后,再颠倒把链表的顺序,恢复原样。
算法1的代码:
c++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(!headA||!headB)
return NULL;
ListNode *p = headB;
while(p->next){
p=p->next;
}
ListNode *tailB = p;
tailB->next=headB; ListNode *p1,*p2;
p1=headA;
p2=headA;
while(p1){
p1=p1->next;
p2=p2->next;
if(p1){
p1=p1->next;
}else{
tailB->next = NULL;
return NULL;
}
if(p1==p2){
break;
}
}
if(p1==NULL){
tailB->next = NULL;
return NULL;
}
p2=headA;
while(p1!=p2){
p2=p2->next;
p1=p1->next;
}
tailB->next = NULL;
return p2; }
};
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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- 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. ...
随机推荐
- Excel 自动更正
当有复杂的字段需要重复填写怎么办呢,比如××银行卡号,××电话号码,××公司地址等.可以使用excel的"自动更正"功能解决. 1. Excel 2010的自动更正选项在哪里呢 2 ...
- UVA2636
理解;类似我们离散的命题 因为只有一个是坏的 超过一个人说你坏 你一定就是坏的 有人说你对 你就对了 分为两种情况 1.说你对的是好的 他的判断是正确的 2.说你对的人 是坏的 他的判断是错误 ...
- Rails 实现多对多自连接
1. 先生成关联代码 class WorkConnect < ActiveRecord::Base belongs_to :working, class_name: "Company& ...
- jstl param url redirect import
import标签 import标签用来导入其他页面 属性: * url :引入页面的路径 * context :工程名 * var :将引入的页面保存到一个变量中 * scope :保存到一个作用域中 ...
- hmm
http://www.cnblogs.com/mindpuzzle/p/3653043.html http://en.wikipedia.org/wiki/Viterbi_algorithm http ...
- Java 读写XML
package dome4jTest; import java.io.FileWriter; import java.io.IOException; import java.net.URL; impo ...
- VS2013无法连接到SqlServer的问题解决
在本机安装Vs2013后,安装Sqlserver2012数据库,在VS开发时,数据库一直查询不到 点击刷新后,看不见本机Sql服务器 最初检查防火墙设置,发现添加sqlservr.exe依然不起作用 ...
- VC++ CString类完美总结(整理)
CString 是编程中一种非常有用的数据类型,它是MFC中的一个类,很大程度上简化了MFC中的许多字符串的操作. CString位于头文件afx.h中. ①.CString 类对象的初始化: CSt ...
- node07-http
目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http node08-exp ...
- Mac下Boost环境搭建
Boost,一个功能强大.跨平台.开源而且免费的C++程序库,可以在其官网了解更多:http://www.boost.org,C++标准经过不断的升级完善,现在已经功能越来越吸引人了,Boost开发过 ...