[LeetCode160]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.
思路:求一个单链表交集的首结点
方法一:找出两个链表的长度差n,长链表先走n步,然后同时移动,判断有没有相同结点
方法二:两个链表同时移动,链表到尾部后,跳到链表头部,相遇点即为所求
代码:
方法一:
/**
* 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) {
//方法一
ListNode *p = headA;
ListNode *q = headB;
if(!p || !q) return NULL;
while(p && q && p != q)
{
p = p->next;
q = q->next;
if(p == q)
return p;
if(!p)
p = headB;
if(!q)
q = headA;
}
return p;
}
};
方法二:
/**
* 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 == NULL || headB == NULL) return NULL;
ListNode *p = headA;
ListNode *q = headB;
int countA = , countB = ;
while (p->next)
{
p = p->next;
++countA;
}
while (q->next)
{
q = q->next;
++countB;
}
if (p != q) return NULL;
if (countA > countB)
{
for (int i = ; i < countA - countB; ++i)
headA = headA->next;
}
else if (countB > countA)
{
for (int i = ; i < countB - countA; ++i)
headB = headB->next;
}
while (headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA; }
};
[LeetCode160]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 ... 
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
		Write a program to find the node at which the intersection of two singly linked lists begins. For ex ... 
随机推荐
- Java提高篇(三二)-----List总结
			前面LZ已经充分介绍了有关于List接口的大部分知识,如ArrayList.LinkedList.Vector.Stack,通过这几个知识点能够对List接口有了比較深的了解了.仅仅有通过归纳总结的知 ... 
- char数组和String互转
			char ch[100];string str; 把char*(c类型的string)数组转换为string:str = ch; //即可str.assign(ch); //也可 把string类型转 ... 
- .net Mvc文件下载的功能,大文件下载完成之后修改数据库功能
			原文:.net Mvc文件下载的功能,大文件下载完成之后修改数据库功能 我服务器上文件只能下载一次,下载了之后就不能下载了,大文件或网速不好时,可能服务端文件流发送完了,客户端还没下载完,导致下载失败 ... 
- 为何要fork()两次来避免产生僵尸进程?
			为何要fork()两次来避免产生僵尸进程? 当我们只fork()一次后,存在父进程和子进程.这时有两种方法来避免产生僵尸进程: 父进程调用waitpid()等函数来接收子进程退出状态. 父进程先结 ... 
- class 添加样式,删,开关 【选择】addClass,removeClass,toggleClass
			<1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ... 
- WPF自定义ListBox样式
			<!--竖向--> <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}"> ... 
- LNK1207: incompatible PDB format in********
			LNK1207: incompatible PDB format in******** VC中错误:LINK : fatal error LNK1207: incompatible PDB forma ... 
- zabbix监控nginx连接状态(转)
			zabbix监控nginx zabbix可以监控nginx的状态,关于一个服务的状态可以查看服务本身的状态(版本号.是否开启),还应该关注服务能力(例如以nginx的负载效果:连接数.请求数和句柄数) ... 
- poj3667(线段树)
			题目连接:http://poj.org/problem?id=3667 题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 2 a b:将[a,a+b-1]的房间清空 线段树操作:upd ... 
- PSU 离11.2.0.3.0 -> 11.2.0.3.11 如果解决冲突的整个
			Oracle rdbms 扑灭psu离11.2.0.3.0升级到11.2.0.3.11 参考patch :18522512 停止应用,停止听音乐并DB,将db的oracle_home在下面OPatch ... 
