(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 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.
题目要求:
求两个链表的交点,如果没有,则返回NULL
要求O(n)的时间复杂度和O(1)的空间复杂度
解题思路:
1、如果不考虑空间复杂度,可以用set容器记录第一个链表的所有结点,依次遍历第二个链表,第一个存在set中的结点即为交点,否则不存在。
2、第一个链表长为x,第二个链表长为y,假设x>y,让第一个链表指针先走x-y步,(这样两个链表指针就长度对齐),然后两个链表指针一起走,如果遇到对应相等,则为交点,否则不存在。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getLength(ListNode *head){
int i=;
for(ListNode *p=head;p!=NULL;p=p->next)
i++;
return i;
} ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL || headB==NULL) return NULL;
int lenA=,lenB=;
ListNode *pA,*pB;
pA=headA;
pB=headB; lenA=getLength(pA);
lenB=getLength(pB); if(lenA>lenB){
for(int i=lenA-lenB;i>;i--)
pA=pA->next;
} if(lenB>lenA){
for(int i=lenB-lenA;i>;i--)
pB=pB->next;
} while(pA!=pB){
pA=pA->next;
pB=pB->next;
} if(pA==pB)
return pA;
else
return NULL;
}
};
(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 ...
- 【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(Java实现)
这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...
- LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- (LinkedList)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 83)Remove Duplicates from Sorted Lists
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode 题解]:Intersection of Two Linked Lists
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- LeetCode题解之Intersection of Two Linked Lists
1.题目描述 2.问题分析 使用unordered_set 将链表A中的节点地址全部插入,然后使用链表B中的每个节点在A中查找. 3.代码 ListNode *getIntersectionNode( ...
- [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 ...
随机推荐
- Codeforces.809E.Surprise me!(莫比乌斯反演 虚树)
题目链接 \(Description\) 给定一棵树,求\[\frac{1}{n(n-1)/2}\times\sum_{i\in[1,n],j\in[1,n],i\neq j}\varphi(a_i\ ...
- 构造Huffman以及实现
构造Huffman 题目 在作业本上分别针对权值集合W=(6,5,3,4,60,18,77)和W=(7,2,4,5,8)构造哈夫曼树,提交构造过程的照片 错误回答 错误原因:遵循左边小于根右边大于根的 ...
- linux下使用cronjob定时执行php脚本
在linux中输入命令 crontab -e 然后使用vim的命令编辑打开的文件,输入 * * * * /usr/bin/php -f /home/userxxx/update.php 保存,退出,好 ...
- 微信小程序 scroll-view隐藏横向滚动条
::-webkit-scrollbar { width: 0; height: 0; color: transparent; }
- 不错的SDL源码分析
SDL源码分析 1:初始化(SDL_Init()) 2:窗口(SDL_Window) 3:渲染器(SDL_Renderer) 4:纹理(SDL_Texture) 5:更新纹理(SDL_UpdateT ...
- 将代码库从 SVN 迁移至 Git 并保留所有 commit 记录
公司内部原本使用 SVN 进行版本控制,但随着 Github 的流行我个人的代码管理习惯逐渐转变.虽然公司项目并非开源,SVN 所具有的标准 trunk / branches / tags 结构完全够 ...
- MultipartFile+nio上传文件
import java.io.File;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths; ...
- DocumentManager 在标签位置显示气泡框 z
关于DevExpress DockManager下的DocumentManager头部标签如何显示气泡框,类似Visual studio那样显示文件的路径,如下图所示,------- 方式很简单,从工 ...
- 纯文本抽出程序库DMC TEXT FILTER
因需而生,红樱枫为文本转换市场领航 --纯文本抽出程序库DMC TEXT FILTER,从需求中把握平衡 在高度数字化的今天,数字图书馆已经成为非常多人查询资料的有效途径.然而即使在畅通的宽带搜寻中一 ...
- [runtime] initialize方法讲解
+ (void)initializeDescription(描述) Initializes the class before it receives its first message. 在这个 ...