【LeetCode从零单排】No.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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
代码
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {if(headA==null || headB==null) return null;int Aindex_length=getLength(headA);int Bindex_length=getLength(headB);int dis=Math.abs(Aindex_length-Bindex_length);ListNode Aindex=headA;ListNode Bindex=headB;if(Aindex_length>=Bindex_length){for(int i=0;i<dis;i++){Aindex=Aindex.next;} while(Bindex!=null){if(Aindex.val==Bindex.val){return Aindex;}else{Aindex=Aindex.next;Bindex=Bindex.next;}}}Aindex=headA;Bindex=headB;if(Aindex_length<Bindex_length){for(int i=0;i<dis;i++){Bindex=Bindex.next;} while(Aindex!=null){if(Aindex.val==Bindex.val){return Aindex;}else{Aindex=Aindex.next;Bindex=Bindex.next;}}}return null;}public int getLength(ListNode head){ListNode index=head;int length=1;while(index.next!=null){index=index.next;length++;}return length;}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No.160 Intersection of Two Linked Lists相关推荐
- LeetCode(160): Intersection of Two Linked Lists
Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...
- [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 python3 简单题160. Intersection of Two Linked Lists
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...
- [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 OJ 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. Fo ...
- 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 ...
- letecode [160] - Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. 题目大意: ...
- 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)
题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...
最新文章
- Chemistry.AI | 基于非线性激活的多层感知器预测分子特性
- 智能车竞赛技术报告 | 智能车视觉 - 三江学院 - 识别不别
- tensorflow 的 Batch Normalization 实现(tf.nn.moments、tf.nn.batch_normalization)
- 怎么让手机变成震动器_详解iPhone 手机标配的两种技术:线性马达和3Dtouch,有多好用?...
- HybridTime - Accessible Global Consistency with High Clock Uncertainty
- 【CodeForces - 608D】Zuma(区间dp)
- easyui修改css样式,修改easyui的easyloader的默认css目录路径
- Codeforces Round #260 (Div. 1) 	A - Boredom DP
- fastdfs-zyc监控系统的使用
- “打砖块”H5游戏源码
- 1小时就会的测试用例【直播推流/拉流】
- 环丙沙星大鼠血清白蛋白纳米粒|甲硝唑小麦麦清白蛋白纳米粒|雷替曲塞乳清白蛋白纳米粒(科研级)
- android设置管理员权限设置,android 获取超级管理员权限,进行锁屏、恢复出厂设置...
- VMware收购Wavefront增强云管理产品组合
- css实现3D长方形,可旋转
- Acrobat如何将PDF拆分为多个文档
- Java 调用第三方接口,实战来了!
- 全年营收预增40%,奈雪的茶背后的喜与忧
- 二、三级等保建议安全设备及其主要依据(毫无保留版)
- 家里安装了新的宽带,大部分电视和电影不能观看需要再次购买VIP,你怎么看
热门文章
- leetcode 101. 对称二叉树 递归解法 c语言
- mysql-5.7.18-winx64 安装 net start mysql 发生系统错误2
- 清华计算机系媒体所,清华大学计算机系媒体所《时光机》新年联欢会小结
- mysql schedule event,MySQL 定时器event
- php在页面循环输出标签,自定义页面循环
- 如何用js获取外联css,内联外联CSS和JS
- 计算机应用能力测试攻略,计算机应用能力测试题(一).doc
- 为什么正则化可以起到对模型容量进行控制_论文解读 | 基于正则化图神经网络的脑电情绪识别...
- **Java有哪些悲观锁的实现_面试4连问:乐观锁与悲观锁的概念、实现方式、场景、优缺点?...
- jQuery 一次定时器_年薪百万之路--第五十一天 jQuery(上)