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.
/**
* 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=,lenB=;
ListNode *tempA=headA;
ListNode *tempB=headB;
while(tempA){
lenA++;
tempA=tempA->next;
}
while(tempB){
lenB++;
tempB=tempB->next;
}
while(lenA>lenB){
headA=headA->next;
lenA--;
}
while(lenB>lenA){
headB=headB->next;
lenB--;
}
while(headA != headB){
headA=headA->next;
headB=headB->next;
}
return headA; }
};
Intersection of Two Linked Lists两链表找重合节点的更多相关文章
- [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 ...
- 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. For ex ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- 【LeetCode】Intersection of Two Linked Lists(相交链表)
这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...
- Leetcode 160 Intersection of Two Linked Lists 单向链表
找出链表的交点, 如图所示的c1, 如果没有相交返回null. A: a1 → a2 ↘ ...
- 160 Intersection of Two Linked Lists 相交链表
编写一个程序,找到两个单链表相交的起始节点.例如,下面的两个链表:A: a1 → a2 ↘ ...
- [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(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
随机推荐
- 阿里云 Aliplayer高级功能介绍(九):自动播放体验
基本介绍 经常会碰到客户询问,为什么我设置了autoplay为true,但是没有自动播放,每次都要向客户解释这个是浏览器从用户体验角度考虑做的限制,客户会继续询问那我要怎么做? 针对这个问题Alipl ...
- html常用标签详解3-a标签
a标签 1.a标签的属性 a标签属于行内元素标签,双标签<a></a> href:a标签的跳转地址 target:打开方式(_self自身:_blank:新窗口) title: ...
- linear-gradient
http://jsbin.com/mocojehosa/edit?html,css,output https://developer.mozilla.org/zh-CN/docs/Web/CSS/li ...
- Android SDK上手指南:下一步学习方向
Android SDK上手指南:下一步学习方向 2014-02-28 11:01 核子可乐 译 51CTO 字号:T | T 到目前为止,我们已经介绍过的知识足以帮助大家从非常理想的起点开始进行And ...
- python编程:从入门到实践学习笔记
python编程:从入门到实践学习笔记 原文地址:https://blog.csdn.net/qq_35554125/article/details/79548192 [day 1]python编程: ...
- TZOJ 4024 游戏人生之梦幻西游(连续子段和绝对值最小)
塔神酷爱玩梦幻西游这款游戏,这款游戏以著名的章回小说<西游记>故事为背景,透过Q版的人物,营造出浪漫的网络游戏风格.塔神以追求天下无敌为目标,从一个默默无闻的菜鸟,打拼到了登峰造极的大师, ...
- uwsgi: invalid option -- 'x'
安装:pip install uwsgi 启动:uwsgi -x 'uwsgi.xml'报错:uwsgi: invalid option -- 'x' 原因:centos下,在没有安装libxml2时 ...
- 华为云DevCloud一枝独秀
DevOps,是Development和Operations的组合词,是指一组过程.方法与系统的统称,用于促进开发.技术运营和质量保障部门之间的沟通.协作与整合.DevOps是一种重视“软件开发人员( ...
- 前端(jQuery)(2)-- JQuery选择器和事件
1.选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- mybatis深入理解(七)-----MyBatis缓存机制的设计与实现
缓存设计 MyBatis将数据缓存设计成两级结构,分为一级缓存.二级缓存: 一级缓存是Session会话级别的缓存,位于表示一次数据库会话的SqlSession对象之中,又被称之为本地缓存.一级缓存是 ...