160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists【easy】
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.
解法一:
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// write your code here
if(headA == NULL || headB == NULL)
return NULL;
ListNode* iter1 = headA;
ListNode* iter2 = headB;
int len1 = ;
while(iter1->next != NULL)
{
iter1 = iter1->next;
len1 ++;
}
int len2 = ;
while(iter2->next != NULL)
{
iter2 = iter2->next;
len2 ++;
}
if(iter1 != iter2)
return NULL;
if(len1 > len2)
{
for(int i = ; i < len1-len2; i ++)
headA = headA->next;
}
else if(len2 > len1)
{
for(int i = ; i < len2-len1; i ++)
headB = headB->next;
}
while(headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA;
}
};
先算长度,然后长的先走差值步,然后同时走
解法二:
public class Solution {
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
// get the tail of list A.
ListNode node = headA;
while (node.next != null) {
node = node.next;
}
node.next = headB;
ListNode result = listCycleII(headA);
node.next = null;
return result;
}
private ListNode listCycleII(ListNode head) {
ListNode slow = head, fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return null;
}
slow = slow.next;
fast = fast.next.next;
}
slow = head;
fast = fast.next;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
先弄成环,转换为找环的入口问题,找到之后再断开环

找环的问题解法可以参见(142. Linked List Cycle II【easy】)
160. Intersection of Two Linked Lists【easy】的更多相关文章
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 380. Intersection of Two Linked Lists【medium】
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- [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]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 ...
- 【LeetCode】160. Intersection of Two Linked Lists
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
随机推荐
- Servlet 3.1 规范
在线版目录 Servlet3.1规范翻译——前言 Servlet3.1规范翻译——概览 Servlet3.1规范翻译——Servlet Context Servlet3.1规范翻译——Response ...
- 【线段树】bzoj3747 [POI2015]Kinoman
题解:http://www.cnblogs.com/zyfzyf/p/4105184.html 一.下传标记写法 #include<cstdio> #include<algorith ...
- 10.2(java学习笔记)JDBC事务简述
一.事务 事务是指作为一系列操作组成的一个整体,该整体只有两种状态,要么全部执行,要么全部不执行. 当组成这个事务的所有语句都执行成功则该事务执行,只要有一条语句执行失败则该事务不执行. 假设这里有一 ...
- 使用MR求解多个矩阵的乘积之后
首先介绍涉及到的知识点,如下: 1)value的类型是IntArrayWritable,将整型数组值取出的方法有两种. a.其一,就是使用value的toArray()方法,返回值是一个Object ...
- java.sql.SQLException: Io 异常: Got minus one from a read call
博客分类: Oracle Tomcat服务器下的应用连接Oracle时报错,出现以下异常: java.sql.SQLException: Io 异常: Got minus one from a r ...
- 怎么在VS2010中打开VS2013的项目
其实VS2010与VS2013上的sln文件没有本质的区别.打不开的原因是什么呢?其实原因很简单,两者开头的软件信息不同.因此造成低版本VS的不识别. VS2013版本vs.sln文件开头的软件信息: ...
- 【转载】网络攻击技术(三)——Denial Of Service & 哈希相关 & PHP语言 & Java语言
找到了这个系列的原始作者: http://www.cnblogs.com/rush/archive/2012/02/05/2339037.html 最近网络安全成了一个焦点,除了国内明文密码的安全事件 ...
- Kubernetes用户指南(一)--快速开始、使用k8s配置文件
一.快速开始 1.启动一个简单的容器. 一旦在container中打包好应用并将其commit为image之后,你就可以将其部署在k8s集群上. 一个简单的nginx服务器例子: 先决条件:你需要拥有 ...
- 【千纸诗书】—— PHP/MySQL二手书网站后台开发之项目设计
前言:这个项目是毕设时候做的,我负责后台数据操作部分,已经很久了,这次回顾这部分,是为了复习PHP和MySQL的知识,正好现在在公司也负责的是后台管理系统的业务.第一篇[项目概况]附上毕业论文部分节选 ...
- 伟大的UHD编解码器的辩论:谷歌VP9与HEVC / H.265
伟大的UHD编解码器的辩论:谷歌VP9与HEVC / H.265 截至今天,伟大的UHD编解码器的争论涉及两个主要参与者:谷歌VP9和HEVC / H.265. 哪一个获得成功并where-invol ...