【一天一道LeetCode】#160. Intersection of Two Linked Lists
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:两个单向链表,有一段重复区域,找出其中的第一个交叉节点。
解题思路:题目要求时间复杂度O(n)和空间复杂度O(1),所以利用辅助空间的方法都不行。
如果两个链表会交叉,那么他们的最后一个节点肯定相同,如果是双向链表,可以从尾节点开始,找到第一个出现分离的节点即可。可是,题目要求不能破坏初始链表。这种方法也行不通。
如果两个链表的长度一样的话,从头开始往后,可以找到第一个交叉点。
记链表的长度为len1和len2,可以让长链表先走abs(len1-len2)步,再两个一起往后找。即可找到第一个交叉点。
具体解释见代码:
/**
* 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) {
int lenA = getlength(headA);//统计链表A的长度
int lenB = getlength(headB);//统计链表B的长度
int diflen = lenA-lenB;//计算差值
//lenA<lenB的情况
ListNode *longList = headA;//lenA<lenB的情况
ListNode *shortList = headB;
//lenA>lenB的情况
if(diflen<0)
{
longList = headB;
shortList = headA;
diflen = -diflen;
}
while(diflen>0&&longList!=NULL)//让长链表先走diflen步
{
longList = longList->next;
diflen--;
}
while(longList!=NULL&&shortList!=NULL)//两个链表一起往后走
{
if(longList->val == shortList->val) return longList;//找到交叉节点
else{
longList = longList->next;
shortList = shortList->next;
}
}
return NULL;
}
int getlength(ListNode *head)
{
int n = 0;
ListNode *phead = head;
while(phead!=NULL)
{
phead = phead->next;
n++;
}
return n;
}
};
【一天一道LeetCode】#160. Intersection of Two Linked Lists的更多相关文章
- [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. For ex ...
- [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
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Java for 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 求两个链表的起始重复位置 --------- java
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Java [Leetcode 160]Intersection of Two Linked Lists
题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- (链表 双指针) 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 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
随机推荐
- Python3 运算符
装载自:https://www.cnblogs.com/cisum/p/8064222.html Python3 运算符 什么是运算符? 本章节主要说明Python的运算符.举个简单的例子 4 +5 ...
- Python与C的简单比较(Python3.0)
Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...
- Unique-paths (动态规划)
题目描述 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...
- Failed to connect to GitHub to update the CocoaPods/Specs specs repo - Please check if you are offline, or that GitHub is down
Failed to connect to GitHub to update the CocoaPods/Specs specs repo - Please check if you are offli ...
- mysql catalog的名字
def 算是一个一点卵用都没有的知识点 然后tmd各个版本不同 用这个语句查 SELECT * FROM information_schema.SCHEMATA where schema_name=' ...
- Java高新技术第一篇:类加载器详解
首先来了解一下字节码和class文件的区别: 我们知道,新建一个Java对象的时候,JVM要将这个对象对应的字节码加载到内存中,这个字节码的原始信息存放在classpath(就是我们新建Java工程的 ...
- Win10 下Cmake编译配置 Opencv3.1 + Cuda7.5 + VS2013
折腾了三天终于配置成功了,在此写下编译配置的全部步骤和遇到的很多坑. 整体介绍: OpenCV 中 CUDA 实现的函数还不是太多,使用前要在OpenCV的官网上确认以下你想要的功能是否已经实现,否则 ...
- MySQL/MariaDB 在插入数据的时候提示 Incorrect string value
现象 今天开新工程,建表的时候弹出这玩意: what's this? 看起来好像是说我传入的内容不对? 可是仔细看看内容,没乱码,标准的中文字符串.想来是编码问题. 经过修改编码后,解决了问题. 解决 ...
- 服务器&阵列卡&组raid 5
清除raid信息后,机器将会读不到系统, 后面若进一步操作处理, raid信息有可能会被初始化掉,那么硬盘数据就有可能会被清空, 导致数据丢失, 否则如果只是清除raid信息,重做raid是可以还原系 ...
- 容器化现有ASP.NET MVC 5应用
.NET Core的出现使得ASP.NET应用在Linux环境下使用变得更加普及.而配合上Docker容器,令ASP.NET应用的布署与管理也变得更加方便.在新的项目中运用ASP.NET Core无可 ...