题目

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.

题目要求取两个链表的交点,而且时间复杂度必须是O(n),所以就不能用嵌套循环的方法。用了如下方法,先计算两个链表的各自长度,将长链表节点向下移动两链表长度差,再计算。

代码

/*** 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;}
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No.160 Intersection of Two Linked Lists相关推荐

  1. 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 ...

  2. [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 ...

  3. leetcode python3 简单题160. Intersection of Two Linked Lists

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. letecode [160] - Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. 题目大意: ...

  9. 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 ...

最新文章

  1. Chemistry.AI | 基于非线性激活的多层感知器预测分子特性
  2. 智能车竞赛技术报告 | 智能车视觉 - 三江学院 - 识别不别
  3. tensorflow 的 Batch Normalization 实现(tf.nn.moments、tf.nn.batch_normalization)
  4. 怎么让手机变成震动器_详解iPhone 手机标配的两种技术:线性马达和3Dtouch,有多好用?...
  5. HybridTime - Accessible Global Consistency with High Clock Uncertainty
  6. 【CodeForces - 608D】Zuma(区间dp)
  7. easyui修改css样式,修改easyui的easyloader的默认css目录路径
  8. Codeforces Round #260 (Div. 1) A - Boredom DP
  9. fastdfs-zyc监控系统的使用
  10. “打砖块”H5游戏源码
  11. 1小时就会的测试用例【直播推流/拉流】
  12. 环丙沙星大鼠血清白蛋白纳米粒|甲硝唑小麦麦清白蛋白纳米粒|雷替曲塞乳清白蛋白纳米粒(科研级)
  13. android设置管理员权限设置,android 获取超级管理员权限,进行锁屏、恢复出厂设置...
  14. VMware收购Wavefront增强云管理产品组合
  15. css实现3D长方形,可旋转
  16. Acrobat如何将PDF拆分为多个文档
  17. Java 调用第三方接口,实战来了!
  18. 全年营收预增40%,奈雪的茶背后的喜与忧
  19. 二、三级等保建议安全设备及其主要依据(毫无保留版)
  20. 家里安装了新的宽带,大部分电视和电影不能观看需要再次购买VIP,你怎么看

热门文章

  1. leetcode 101. 对称二叉树 递归解法 c语言
  2. mysql-5.7.18-winx64 安装 net start mysql 发生系统错误2
  3. 清华计算机系媒体所,清华大学计算机系媒体所《时光机》新年联欢会小结
  4. mysql schedule event,MySQL 定时器event
  5. php在页面循环输出标签,自定义页面循环
  6. 如何用js获取外联css,内联外联CSS和JS
  7. 计算机应用能力测试攻略,计算机应用能力测试题(一).doc
  8. 为什么正则化可以起到对模型容量进行控制_论文解读 | 基于正则化图神经网络的脑电情绪识别...
  9. **Java有哪些悲观锁的实现_面试4连问:乐观锁与悲观锁的概念、实现方式、场景、优缺点?...
  10. jQuery 一次定时器_年薪百万之路--第五十一天 jQuery(上)